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,这将 return 来自 wp_posts table 的所有帖子。例如
$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;
?>
我一定是脑残了,我不知道如何从 $wp_query
中获取所有帖子,以便为搜索结果创建小部件过滤器。
$wp_query->posts
只给我将要显示在列表中的帖子,因此,如果 posts_per_page
设置为 10,我只会得到 10 个帖子。我需要它们,这样我就可以对它们进行排序,并根据搜索结果中的所有帖子显示过滤器。
有什么想法吗?
将 args 中的 posts_per_page 参数设置为 -1,这将 return 来自 wp_posts table 的所有帖子。例如
$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;
?>