WordPress query – Order by meta-field value

I have a post with three meta-fields.

add_post_meta($my_post, 'times', $times);

And i would like to query this category and sort the posts by the meta field value of one of them.
The args i use right now are:

Read More
  $args=array(
        'post_type' => 'post',
        'category_name' => 'players',
        'order' => 'DESC', 
        'orderby' => 'meta_value_num',
        'meta_key' => 'times',
        'meta_query' => array(
       array(
           'key' => 'times',
           'value' => 0,
           'compare' => '>=',
       ),
        'posts_per_page'=> '8'
        )
        );
      

Where times is the name of the metafield.The above code isn’t returning anything.

Related posts

Leave a Reply

1 comment

  1. You have 'posts_per_page'=> '8' inside your meta_query argument.

    Change your code into the following:

    $args=array(
        'post_type' => 'post',
        'category_name' => 'players',
        'order' => 'DESC', 
        'orderby' => 'meta_value_num',
        'meta_key' => 'times',
        'meta_query' => array(
            array(
                'key' => 'times',
                'value' => 0,
                'compare' => '>=',
            )
        ),
        'posts_per_page'=> '8'
    );