Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

Any ideas?

like image 583
Caio Mars Avatar asked May 29 '15 14:05

Caio Mars


1 Answers

Set posts_per_page parameter in args to -1, this will return all posts from wp_posts table. 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 } 
like image 183
Gajendra Singh Avatar answered Sep 24 '22 15:09

Gajendra Singh