0

I am trying to order custom fields by state and city. This is for a WordPress website and I am using ACF custom fields.

Find my unsuccessful meta_query below.

  $hotel = new WP_Query( array(
      'post_type'           => 'hotel',
      'meta_query' => array(
          'relation' => 'AND',
   
          'country' => array(
            'meta_key' => 'hotel_address_country',
            'compare' => 'EXISTS',
          ),

          'city' => array(
            'meta_key' => 'hotel_address_city_state',
            'compare' => 'EXISTS',
          ),
        ),

        'orderby' => array(
          'country' => 'DESC',
          'city' => 'ASC',
        ),
      'numberposts' => -1,
      'post_status' => 'publish',
      'public'      => 'true',
      'posts_per_page' => '500',
   ) );
Julie
  • 1
  • 2
  • Does [this other SO post](https://stackoverflow.com/questions/17745334/how-to-order-by-multiple-meta-keys) help with your query to order by multiple meta keys? – wouch Jan 09 '23 at 16:13
  • Thanks, I can do that one. I get stuck when I try to order two meta_values using a meta_query. – Julie Jan 10 '23 at 02:04

1 Answers1

0

I haven't had a chance to test. But I got this feedback and think it will work in case anyone else is looking for the solution...

$hotel = new WP_Query( array( 'post_type' => 'hotel', 'numberposts' => -1, 'meta_query' => array( 'relation' => 'AND', 'country_clause' => array( 'key' => 'hotel_country', 'compare' => 'EXISTS', ), 'city_clause' => array( 'key' => 'hotel_city', 'compare' => 'EXISTS', ),

    ),
    'orderby'        => array(
        'country_clause' => 'ASC',
        'city_clause'    => 'ASC',
    ),

    // 'post_status' => 'publish',
    // 'public' => 'true',
    'posts_per_page' => '500',
)

);

Julie
  • 1
  • 2