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 in PHP by kartik
• 11,890 points
33 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 by Niroj
• 23,950 points

Related Questions In PHP

0 votes
1 answer

How can I handle the warning of file_get_contents() function in PHP?

Hello @kartik, This is fairly simple: if (!$data = ...READ MORE

answered Apr 7 in PHP by Niroj
• 23,950 points
61 views
0 votes
1 answer

How to get the client IP address in PHP ?

Hello, Here is a code sample of a good ...READ MORE

answered Apr 8 in PHP by Niroj
• 23,950 points
245 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27 in PHP by Niroj
• 23,950 points
36 views
0 votes
1 answer

How to get thumbnail of youtube video from youtube API?

Hii @kartik, If you want the biggest image ...READ MORE

answered Apr 1 in PHP by Niroj
• 23,950 points
123 views
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 in Web Development by anonymous
• 11,890 points
48 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 in Web Development by Niroj
• 23,950 points

edited Jan 21 by Niroj 70 views
0 votes
1 answer

What is data binding in AngularJS?

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

answered Jan 23 in Web Development by Niroj
• 23,950 points
39 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 in PHP by Niroj
• 23,950 points
71 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 in PHP by Niroj
• 23,950 points
28 views