Filter by one custom field, order by another?

You could use the query to filter the content as you intended by using the ‘meta_query’ with filtering options, and for the order part, just add/modify the following parameters:

  • ‘orderby’ => ‘meta_value’
  • ‘meta_key’ => ‘location_level1_value’
  • ‘order’ => ‘ASC’

    $wp_query = new WP_Query( array (
        'post_type'      => 'listing',
        'posts_per_page' => '9',
        'post_status'    => 'publish',
        'meta_query'     => array(
            array(
                'key'       => 'gateway_value',
                'value'     => 'Yes',
                'compare'   => '!='
            )
        ),
        'orderby'  => 'meta_value',            // this means we will be using a selected 
                                               // meta field to order
    
        'meta_key' => 'location_level1_value', // this states which meta field 
                                               // will be used in the ordering, 
                                               // regardless of the filters
        'order'    => 'ASC',
        'paged'    => $paged
        )
    );
    

Leave a Comment