I am having an issue on an archive page. Everything seems to be working fine, exept the fact that "the_query" returns only two posts, when it should return 10 of them.

I wanted to debug the query so I used the following line :

echo "<hr>Last SQL-Query: {$the_query->request}";

Which returns :

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_postmeta.meta_key = 'client' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value DESC LIMIT 0, 2

When I run it in phpMyAdmin, it returns 2 IDs. When I remove the end (" LIMIT 0, 2"), it then returns the 10 IDs I was expecting.

Where does this limit come from ? I am quite suprised !

Here is my php code :

        $the_query = new  WP_Query
                        'meta_key'=> 'client',
                        'orderby' => 'meta_value',
                        'order'   => 'DESC'

echo "<hr>Last SQL-Query: {$the_query->request}";

I then have another bit that displays posts, but it works fine since the two posts are displayed properly.

Thank you for your help !


Use the posts_per_page parameter in your WP_Query to show a given number of posts.

When you don't provide this parameter, the query takes default value which can be set in backend on https://example.com/wp-admin/options-reading.php.

| improve this answer | |
  • Even if it is not a paged query ? – Grégoire Llorca Oct 25 '16 at 9:25
  • 1
    I ran a test and it works. I used the value -1 to make it unlimited. – Grégoire Llorca Oct 25 '16 at 9:49
  • Even if it' n paged query, default value is taken from settings. – Aniruddha Gawade Oct 25 '16 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.