WP_Query - Order results by meta value

  • I've checked around and haven't seen an answer which works as of yet. I have a WP_Query with the following arguments:

    $args = array(
        'post_status' => 'publish',
        'post_type' => 'listing',
        'meta_key' => 'client_feedback_score',
        'orderby' => 'client_feedback_score',
        'order' => 'DESC'
    );
    
    $query = new WP_Query($args);
    

    I want to order the results by the custom post field client_feedback_score, lowest to highest. But this doesn't seem to work... can anyone point me in the right direction?

    EDIT (SOLVED):

    Thanks to Milo's response, here's the working code for ordering by a numerical meta value:

    $args = array(
        'post_status' => 'publish',
        'post_type' => 'listing',
        'meta_key' => 'client_feedback_score',
        'orderby' => 'meta_value_num',
        'order' => 'DESC'
    );
    
  • Milo

    Milo Correct answer

    9 years ago

    orderby should be meta_value_num, or meta_value, not the name of the key. See WP_Query orderby parameters.

    Worked a treat, cheers mate.

    Thanks! You're my timesaver! Nowhere on WPCodex isn't mentioned about 'meta_value_num'.

    Small note, if the `meta_key` does not exist for the post yet, the post will be ignored.

    is `meta_value_num` a custom post meta that you can assign to post types? I need to be able to edit this value per post.

    @RobBenz See Custom Fields to learn about post meta.

    thank you, I am familiar with custom post meta. I want to add a meta box to WC products, called `search_order` or something. so that when the search results are displayed: I can have the ability to control the order of their display. should I call the custom post meta that I am adding `meta_value_num`

    or does `meta_value_num` just take the value of whatever 'meta_key' => 'search_order' is based on the post

    @RobBenz `meta_value_num` is telling WP to order the results by a numeric meta value. It's not the key name.

    how do i change `meta_value_num` so that a particular post will "rank" higher up the search results

    @RobBenz You should probably start a new question on this topic. Step 1 Assign whatever value you want to your meta key, let's call it `my_meta_key`, for each post to generate whatever order you want. Step 2 You set `meta_key` in your query to `my_meta_key`. This tells WordPress to query for posts that have that key. Step 3 Set `orderby` in your query to `meta_value_num`. This tells WordPress to not only query posts with `my_meta_key`, but to also order numerically by that key. That query argument is the only place you will ever see or use `meta_value_num`.

    @adamj is it any workaround this? I need sort all posts, regardless that meta_key exist for the post or not.

    @unbreak the simple solution is to parse over all the posts and add the `meta_key` to them prior to running the order query

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM