0

I am querying for most popular posts by custom view count by using the custom query below,

<?php 
   $mostpopular_args=array(
       'post_type'          => 'post',
       'orderby'            => 'meta_value_num',
       'meta_key'           => 'view_count',
       'posts_per_page'     => 2,
   );
   <?php $mostpopular_pick = new WP_Query($mostpopular_args); ?>
?>

However, this is not working. As suggested by an expert, I ran $mostpopular_pick->requestand it seems that wordpress is ordering posts by 'menu_order' instead of meta_value_num. Here is what it returned,

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 = 'view_count')
GROUP BY wp_posts.ID
ORDER BY wp_posts.menu_order,
         wp_postmeta.meta_value DESC LIMIT 0,
                                           2

And also, though I limited post count to 2, it is still returning 4 posts.

Whats going on, any Idea?

  • I remember this issue. Clear all caches, disable all plugins and test your code on one of the bundled themes. Then, re-enable your plugins one by one and test after each one. Finally re-enable your theme. Check when the query fails. This should give you an idea what is causing the issue. Also, enable debug to catch any errors. What is alarming, in theory your query should work, something external is making it fail. – Pieter Goosen Mar 23 '15 at 5:57
  • Woah! Thank you so much @Pieter Goosen, Issue resolved. Post Types Order plugin by NSP Code is altering the query, and Sticky posts are the invincible enemies. Can you post this suggestion as an answer? I would love to select this as an answer :) – Naresh Devineni Mar 23 '15 at 6:58
  • Will most certainly do. Glad you got this resolved. Enjoy – Pieter Goosen Mar 23 '15 at 7:09
0

Your code should work in theory. Your failure is not caused by the code in your question but rather due to external factors. Something else is causing this failure

DEBUGGING THIS ISSUE

  • Turn on debug and check for obvious errors and warnings. This should be a good indication on the soundness of all your code on a particular page

  • Clear all caches, not just browser cache, but any cache plugin

  • Disable all your plugins and switch to a bundled theme. Check if the error persist. Re-enable your plugins one by one and test after each one. This should give you an indication if a plugin is causing your issue. Lastly, re-enable your theme. By now you should have a clear indication where your problem is coming from.

  • Regarding the amount of posts and the difference, check for stickies.

0

I have checked this and it works fine. Check meta_key or check custom loop if you use it elsewhere before this and reset its post data

<?php   $array = array(
    'post_type'=>'post',
    'posts_per_page'=>4,
    'order'=>'DESC',
    'orderby'=>'meta_value_num',
    'meta_key'=>'_count-views_all'); 

$the_query = new WP_Query($array);
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
     //Post loop started
}}

?>

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.