WordPress: How to get all posts from $ wp_query in search results?

I have to be dead brain, I canโ€™t figure out how to get ALL messages from $wp_query so that I can create a widget filter for search results.

$wp_query->posts gives me messages to be displayed in the list, so if posts_per_page set to 10, I only get 10 posts. I need all of them, so I can sort them and display a filter based on all the messages from the search results.

Any ideas?

+6
source share
2 answers

Set the posts_per_page parameter in args to -1, this will return all posts from the wp_posts table. eg

 $args = array( 'posts_per_page' => -1, 'post_type' => 'post', ); $the_query = new WP_Query( $args ); 

Now you can scroll and receive messages

 while ( $the_query->have_posts() ) { // go ahead } 
+9
source

Display a filter based on all posts from 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; ?> 
0
source

Source: https://habr.com/ru/post/988169/


All Articles