【发布时间】:2019-12-02 09:59:23
【问题描述】:
我制作了一个页面模板,我可以在其中过滤一些帖子。如果你做一个正常的 WP_Query 一切都很好。但如果我使用 tax_query,它不会显示任何帖子。
为了注册帖子类型和分类,我使用插件“cptui”。
常规的 WP_Query 请求:
$query = new WP_Query(array(
'post_type' => $post->post_name,
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
));
if(!$_POST) { //If not filtered ... then show all
if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
echo "<h5>" . $query->post->post_title . "</h5>";
echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
echo "</div></a>";
endwhile; endif;
}
我阅读了其他帖子,他们说我可以使用 php echo $GLOBALS['query']->request; 来查看 mysql 查询是什么。
我的查询是:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1 = 1
AND wp_posts.post_type = 'vacature'
AND ((wp_posts.post_status = 'publish'))
ORDER BY wp_posts.post_date DESC
LIMIT 6, 6
现在是带有 tax_query 的请求。
tax_query WP_Query 请求:
$query = new WP_Query(array(
'post_type' => $post->post_name,
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'provincie',
'field' => 'slug',
'terms' => 'noord-brabant',
'include_children' => 0
),
),
));
if(!$_POST) { //If not filtered ... then show all
if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
echo "<h5>" . $query->post->post_title . "</h5>";
echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
echo "</div></a>";
endwhile; endif;
}
我现在得到的查询是:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1 = 1
AND (wp_term_relationships.term_taxonomy_id IN (3))
AND wp_posts.post_type = 'vacature'
AND ((wp_posts.post_status = 'publish'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 6, 6
我没有收到回复。
你知道有什么解决办法吗?
模板的完整代码:
<div id="page-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_content(); ?>
<?php
global $post;
$paged = ( get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query(array(
'post_type' => $post->post_name,
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'provincie',
'field' => 'slug',
'terms' => array( 'noord-brabant')
)
)
));
// echo $GLOBALS['query']->request;
if(!$_POST) { //If not filtered ... then show all
if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
echo "<h5>" . $query->post->post_title . "</h5>";
echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
echo "</div></a>";
endwhile; endif;
}
else { //now its filtered
$null = true;
if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
$filterconditions = []; //hier komen alle filtercondities in
$keys = array_keys($_POST);
foreach($keys as $key) {
if($_POST[$key] == "on") {
$filter = explode("_",$key);
$field = $filter[0];
$filter = $filter[1];
$taxonomy = get_field( $field );
$term = get_term_by('term_id', $taxonomy, $field);
array_push($filterconditions, '"' . $filter . '"' . '==' . '"' . $term->name . '"');
}
};
$filterconditions = implode('&&', $filterconditions);
// echo $filterconditions;
if(eval("return $filterconditions;")) {
$null = false;
echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
echo "<h5>" . $query->post->post_title . "</h5>";
echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
echo "</div></a>";
}
endwhile; endif;
if($null) {
echo "Sorry, er zijn geen vacatures gevonden met deze filters";
}
}
$total_pages = $query->max_num_pages;
if ($total_pages > 1 && !$null) {
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
wp_reset_query();
?>
</div>
【问题讨论】:
标签: php wordpress wordpress-theming