php WordPress:如何在搜索结果中从 $wp_query 获取所有帖子?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30531600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WordPress: How do I get all posts from $wp_query in the search results?
提问by ColdTuna
I must be brain dead, I can't figure out how to get ALL posts from the $wp_query
so that I can create a widget filter for the search results.
我一定是脑子死了,我不知道如何从 中获取所有帖子,$wp_query
以便我可以为搜索结果创建一个小部件过滤器。
$wp_query->posts
only gives me the posts that are going to be displayed in the list, so, if posts_per_page
is set to 10, I only get 10 posts. I need them all so I can sort them and display a filter based on all posts from the search results.
$wp_query->posts
只给我将要显示在列表中的帖子,所以,如果posts_per_page
设置为 10,我只会得到 10 个帖子。我需要它们,以便我可以对它们进行排序并根据搜索结果中的所有帖子显示过滤器。
Any ideas?
有任何想法吗?
回答by Gajendra Singh
Set posts_per_page parameter in args to -1, this will return all posts from wp_posts table. for example
将 args 中的 posts_per_page 参数设置为 -1,这将返回 wp_posts 表中的所有帖子。例如
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
);
$the_query = new WP_Query( $args );
Now you can loop through and get posts
现在您可以循环浏览并获取帖子
while ( $the_query->have_posts() ) {
// go ahead
}
回答by RichestSoft
Display a filter based on all posts from the search results.
根据搜索结果中的所有帖子显示过滤器。
<?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;
?>