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?

Read More

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'
);

Related posts

Leave a Reply

2 comments

  1. if your meta value is not numeric for example if it is a date value; than you can add parameter meta_type => DATE.
    Possible values are; 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.

    Query with meta type will be like that:

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