2

I have this code which works fine:

$args = array(
    'post_type' => 'brands',
    'meta_query' => array(
        array(
            'key' => 'br_type',
            'value' => 'Aviation'
    )),
    'posts_per_page' => -1,
    'meta_key' => 'br_name',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'fields' => 'ids'
);

However, I need to order first by ascending order by another custom field "br_category" and then by the name br_name. I am not sure how to implement this.

2
+25

I'm not 100% certain what specific order you're asking for, but you just need to assign an associative array to the 'orderby' value. Here's an example:

$args = array(
    'post_type' => 'brands',
    'meta_query' => array(
        array(
            'key' => 'br_type',
            'value' => 'Aviation'
        ),
        array(
            'key' => 'br_category'
        ),
        array(
            'key' => 'br_name'
        )
    ),
    'posts_per_page' => -1,
    'orderby' => [
        'br_category' => 'ASC',
        'br_name' => 'ASC',
        'br_type' => 'ASC'
    ],
    'order' => 'ASC',
    'fields' => 'ids'
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.