我必须脑力不足,我无法弄清楚如何从$wp_query
获取所有帖子,以便我可以为搜索结果创建一个小部件过滤器。
$wp_query->posts
只会向我提供要在列表中显示的帖子,因此,如果posts_per_page
设置为10,我只会收到10个帖子。我需要它们所以我可以对它们进行排序并根据搜索结果中的所有帖子显示过滤器。
有什么想法吗?
答案 0 :(得分:19)
将args中的posts_per_page参数设置为-1,这将返回wp_posts表中的所有帖子。例如
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
);
$the_query = new WP_Query( $args );
现在你可以循环播放并获得帖子
while ( $the_query->have_posts() ) {
// go ahead
}
答案 1 :(得分:-1)
根据搜索结果中的所有帖子显示过滤器。
<?php
/*pass your search string here example like this ( 's'=>'test' ) */
$args=array('s'=>'test','order'=> 'DESC', 'posts_per_page'=>get_option('posts_per_page'));
$query=new WP_Query($args);
if( $query->have_posts()):
while( $query->have_posts()): $query->the_post();
{
echo $post->post_title;
echo $post->post_content;
}
endwhile;
else:
endif;
?>