0

I have custom WP_Query call where I want to display CPT by one custom field and order them by another:

   $args = array(
        'post_type' => 'nehnutelnost',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'meta_key' => 'podlazie',
        'meta_value' => '2_nadzemne_podlazie',
        'meta_compare' => 'LIKE',
        'meta_key' => 'cislo_bytu',
        'orderby' => 'meta_value',

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

1 Answer 1

0
$q = new WP_Query( array(
    'meta_query' => array(
        'relation' => 'AND',
        'state_clause' => array(
            'key' => 'state',
            'value' => 'Wisconsin',
        ),
        'city_clause' => array(
            'key' => 'city',
            'compare' => 'EXISTS',
        ), 
    ),
    'orderby' => array( 
        'city_clause' => 'ASC',
        'state_clause' => 'DESC',
    ),
) );

You can use this query for ordering, even with more than one meta keys. These are called sub queries.

Please check this link from codex - https://developer.wordpress.org/reference/classes/wp_meta_query/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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