1

Using WP query:

$myquery = new WP_Query( array(
    'posts_per_page' => $instance['count'],
    'meta_key' => 'mywp_post_views_count',
    'orderby' => 'meta_value_num',
    'order' => 'DESC'
) );

Let's get the request:

echo $myquery->request;

It 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_posts.post_type = 'post'
        AND ( wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private )
        AND ( wp_postmeta.meta_key = 'mywp_post_views_count' )
    GROUP BY wp_posts.ID
    ORDER BY wp_postmeta.meta_value+0 DESC
    LIMIT 0, 5

This runs correctly in MySQL, giving me 5 post ID's. But WordPress returns 120 results!

echo '$myquery = ' . count( $myquery->posts );

returns: $myquery = 120

And I've checked when looping through:

 while ( $myquery->have_posts() ) : $myquery->the_post();

It does output the 120 unique posts, rather than the expected 5 posts.

I can confirm:

echo 'Count: '.$instance['count'];

Returns: Count: 5

I am running the latest stable WordPress (4.1), with no plugins apart from ACF (Advanced Custom Fields) - just my custom theme. Disabling this plugin has no effect, and default WordPress theme (2015) has the same issue.

I'm struggling to understand why this is happening.

6
  • What happens if you hard code the amount of posts per page. Do you gt the same output. At this stage I've got a felling this is not an issue with posts per page, but something else, either a custom filter or a typo somewhere in a name causing to return all posts – Pieter Goosen Jan 27 '15 at 9:42
  • I've tried hard coding 5, instead of the $instance['count'] for posts per page - but unfortunately the same issue. – mattpark22 Jan 27 '15 at 11:06
  • 3
    Add 'suppress_filters'=>true to your arguments and report back – Pieter Goosen Jan 27 '15 at 11:12
  • Unfortunately this has no affect either :( – mattpark22 Feb 3 '15 at 12:17
  • Post your complete template. Something else on that page might be causing this. Did you make sure that your values are correct. Have you tried removing the meta_key and orderby parameters. Are you gettting the same result – Pieter Goosen Feb 3 '15 at 12:25
0

Try adding 'nopaging' => false to your query construct.

I just had the same issue and it fixed it for me.

1
  • Downvote? This isn't reddit. Explain. – Athoxx Oct 26 '17 at 11:55

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.