![](/img/trans.png)
[英]Wordpress WP_Query/get_posts where post_title equals returns all results
[英]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.