簡體   English   中英

WordPress:如何從搜索結果中獲取$ wp_query的所有帖子?

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

我必須腦死,我無法弄清楚如何從$wp_query獲取所有帖子,以便我可以為搜索結果創建一個小部件過濾器。

$wp_query->posts只給我將要顯示在列表中的帖子,因此,如果posts_per_page設置為10,我只會收到10個帖子。 我需要它們所以我可以對它們進行排序並根據搜索結果中的所有帖子顯示過濾器。

有任何想法嗎?

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

根據搜索結果中的所有帖子顯示過濾器。

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