How do I get all posts from wp query in the search results in wordpress

0 votes
Hii,

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 for it?
May 8, 2020 in PHP by kartik
• 37,520 points
6,365 views

1 answer to this question.

0 votes

Hello @kartik,

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
}

Hope this work!!

Thank You!

answered May 8, 2020 by Niroj
• 82,840 points

Related Questions In PHP

0 votes
1 answer

How do I get the last inserted ID of a MySQL table in PHP?

Hello @kartik, If you're using PDO, use PDO::lastInsertId. If you're ...READ MORE

answered Oct 22, 2020 in PHP by Niroj
• 82,840 points
2,232 views
0 votes
1 answer

How can I get the classname from a static call in an extended PHP class?

Hello @kartik, __CLASS__ always returns the name of the ...READ MORE

answered Oct 27, 2020 in PHP by Niroj
• 82,840 points
1,038 views
0 votes
1 answer

How can I get ID of the last updated row in MySQL?

Hello @kartik, Suppose, item_id is an integer identity column in items table ...READ MORE

answered Apr 9, 2020 in PHP by Niroj
• 82,840 points
12,214 views
0 votes
1 answer

How to get the current plugin directory in WordPress?

Hello @kartik, This will actually get the result ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,840 points
4,459 views
0 votes
1 answer
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17, 2020 in Web Development by anonymous
• 37,520 points
935 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,840 points

edited Jan 21, 2020 by Niroj 1,144 views
0 votes
1 answer

What is data binding in AngularJS?

Data binding is synchronization of data between the ...READ MORE

answered Jan 23, 2020 in Web Development by Niroj
• 82,840 points
1,121 views
0 votes
1 answer

How do I get a YouTube video thumbnail from the YouTube API?

Hello @kartik, Each YouTube video has four generated ...READ MORE

answered Jun 16, 2020 in PHP by Niroj
• 82,840 points
5,308 views
0 votes
1 answer

How do I get the HTML code of a web page in PHP?

Hello @kartik, If your PHP server allows url ...READ MORE

answered Oct 6, 2020 in PHP by Niroj
• 82,840 points
9,385 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP