my code should show popular posts where the meta key "slider" isn't empty. Have anyone an idea why it doesn't work?

The problem is that this code only shows posts with a slider image. But it ignores the sorting.

$values = array(
  'numberposts' => 10,
  'orderby' => 'meta_value_num',
  'meta_key' => 'count_views',
  'meta_query' => array(
        'key'     => 'slider',
        'compare' => '!=',
        'value'   => ''

$my_posts = get_posts( $values );
global $post;
foreach( $my_posts as $post ) : setup_postdata($post);



The SQL looks correct. (You can substitute new WP_Query for get_posts and echo $my_posts->requests to see it.)

So I am going to guess that the sticky posts are making it look as though the sorting doesn't work. Add 'ignore_sticky_posts' => true to your arguments.

| improve this answer | |
  • I just looked if i have sticky posts. But there is not post where sticky is set to true. – Nepo Znat Mar 24 '14 at 14:50
  • You mean that you have no sticky posts set? – s_ha_dum Mar 24 '14 at 14:59
  • Yes exactly. I have no sticky posts. – Nepo Znat Mar 24 '14 at 15:05

Aren't you missing one array in 'meta_query'? There should be an array inside in array. Take a look at http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

| improve this answer | |
  • Oh thanks you are right. Unfortunately it still doesn't work. – Nepo Znat Mar 24 '14 at 14:51

The plug-in Wordpress Popular Posts could help you here - has some useful shortcoding you can call in PHP on various sidebars and widgets as well as measuring post views and comments.


Not sure about sorting via meta key though as it has been a few years since I used it last.

| improve this answer | |

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.