order by custom field - wordpress

14,157

You would use orderby on the same level as post_type and meta_query in your example.

$args = array(
    'orderby' => 'meta_value',
    'post_type' => 'new',
    'meta_query' => array(
        array(
            'key' => 'over-make',
            'value' => 'Doral',
            'compare' => 'LIKE'
        )
    )

 );
$loop = new WP_Query( $args);

(WordPress Codex: WP_Query)

Share:
14,157
Adam
Author by

Adam

Updated on June 04, 2022

Comments

  • Adam
    Adam almost 2 years

    I'm trying to sort a page of posts by a custom field.

    Here's what I have so far, I'm just not sure how or where to add the orderby

    $args = array(
        'post_type' => 'new',
        'meta_query' => array(
            array(
                'key' => 'over-make',
                'value' => 'Doral',
                'compare' => 'LIKE'
            )
        )
    
     );
    $loop = new WP_Query( $args);