简体   繁体   English

WordPress:如何从搜索结果中获取$ wp_query的所有帖子?

[英]WordPress: How do I get all posts from $wp_query in the search results?

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. $wp_query->posts只给我将要显示在列表中的帖子,因此,如果posts_per_page设置为10,我只会收到10个帖子。 I need them all so I can sort them and display a filter based on all posts from the search results. 我需要它们所以我可以对它们进行排序并根据搜索结果中的所有帖子显示过滤器。

Any ideas? 有任何想法吗?

Set posts_per_page parameter in args to -1, this will return all posts from wp_posts table. 将args中的posts_per_page参数设置为-1,这将返回wp_posts表中的所有帖子。 for example 例如

$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
}

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;
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM