• Hi,

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

    $values = array(
      'numberposts' => 10,
      'orderby' => 'meta_value_num',
      'meta_key' => 'count_views',
      'meta_query' => array(
        array(
          'key'     => 'slider',
          'compare' => '!=',
          'value'   => ''
        )
      )
    );
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi ironman,

    Can you post your full code please?

    At a quick glance I notice that the value of ‘meta_query’ => array( array( <values> ) ) – I’d say this should only be a single array rather than having it nested, one within another.

    This would be the first attempt I’d make, but without seeing how $values is being used I can’t say for sure! ??

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

    (@ironman95)

    Thank you i have overlooked the additional array. ??
    But it still doesn’t work.

    Here is the full code:

    $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_title();
    
    endforeach;
    wp_reset_postdata();

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

    Sorry – just seen the last line of your post please ignore this post! ??

    Try changing the line

    'orderby' => 'meta_value_num',

    to

    'orderby' => 'meta_value meta_value_num',

    This has worked for others?

    Thread Starter Sascha

    (@ironman95)

    Hmm doesn’t work for me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Sort posts by post views and consider meta key’ is closed to new replies.