我制作了一个页面模板, 可以在其中过滤一些帖子。如果你执行正常的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) ((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>
#1
$args=array(
'post_type' => 'your_post_type', 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'DESC', 'posts_per_page' => 6, 'tax_query' => array(
array(
'taxonomy' => 'provincie', 'field' => 'slug', 'terms' => array( 'noord-brabant')
)
)
);
$result = new WP_Query( $args );
// The Loop
if ( $result->have_posts() ) {
while ( $result->have_posts() ) {
$result->the_post();
// this is your loop
}
} else {
// nothing
}
可以尝试上面的代码
#2
你可以使用以下代码获取简单的自定义帖子类型列表:
$args = array(
'post_type' => 'services', 'post_status' => 'publish', 'posts_per_page' => 6, 'orderby’ => 'title', 'order’ => 'ASC', );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_title();
the_excerpt();
endwhile;
wp_reset_postdata();
放置自定义帖子类型名称以检索帖子并根据需要更改while循环
#3
好的, 你可以使用get post方法代替WP Query:
$posts_array = get_posts(
array(
'posts_per_page' => 6, 'post_type' => $post->post_name, // Get POST TYPE FROM $GLOBAL POST
'tax_query' => array(
array(
'taxonomy' => 'provincie', 'field' => 'slug', 'terms' => 'noord-brabant', )
)
)
);
评论前必须登录!
注册