Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP_Query() does not return all entries

Tags:

php

wordpress

I have this query that returns only a few of the entries I have on the table. I have over 10 posts but this query only returns 6. Please help with suggestions

$query = new WP_Query("year=2011&monthnum=09&post_status=publish&post_type=post&orderby=post_date&order=DESC"); while ($query->have_posts()):     $query->the_post();     $title=get_the_Title();                                                                                                                       echo"<p><input type=\"checkbox\" name=\"MyArticle[]\" value=\"".get_the_ID()."\">".get_the_Title()."</p>"; endwhile;                wp_reset_query(); 
like image 385
Enstine Muki Avatar asked Oct 17 '12 20:10

Enstine Muki


People also ask

What is the difference between WP_Query and Get_posts?

The difference between wp_query() and get_posts() is basically the type of loop you want to use to display the posts on the front end. So in the end it mainly boils down to your preference and comfort.

What is Suppress_filters WP_Query?

'suppress_filters' =>true This is the important one, what this does is, it stops filters from altering the query. So pre_get_posts and the build in posts_* filters cannot be used to alter get_posts . This is why in your case you get posts using get_posts and none using WP_Query.

What is WP_Query?

WP_Query is a PHP class for constructing queries to the WordPress database and returning posts, pages, or other custom objects to render on the page. It allows developers to build complex searches while removing the need to write separate SQL queries.


2 Answers

Try adding posts_per_page=-1 to the string of parameters passed to WP_Query.

If that value is not set, then it falls back to use the default posts per page option you have set in Settings >> Reading >> Blog pages show at most.

My guess is that this value is 6 so its returning that many posts since you did not specify a different limit.

like image 75
drew010 Avatar answered Oct 14 '22 05:10

drew010


$args = array(     'post_type' => 'product',     'orderby' => 'ASC',     'posts_per_page'=>-1 ); $wp_query = new WP_Query($args); 
like image 22
Vishal Sadarani Avatar answered Oct 14 '22 05:10

Vishal Sadarani