I have a custom post type called Location with a custom field called City. I also have taxonomies on that post type called Specialties. The specialty filter works just fine with the tax_query, but I can’t get the custom field to filter.

This is not working, and neither is just about everything else I’ve been trying:

$args = array('post_type' => 'location',
    'tax_query' => array(
          array(
          'taxonomy' => 'specialties',
          'field'    => 'slug',
          'terms'    => $specialty,
        )),
    'meta_query' => array(array('city' => $location,'compare' => '=',))
    );

1 Answer
1

Your 'meta_query' value is wrong – it should be:

'meta_query' => array(
    array(
        'key'     => 'city',
        'value'   => $location,
        'compare' => '=',
    )
);

Anyway, you don’t need to use 'meta_query' in this case where you have to filter by just one meta field… So for a little more optimized code, try replacing your $args with the following:

$args = array(
    'post_type' => 'location',
    'tax_query' => array(
        array(
            'taxonomy' => 'specialties',
            'field'    => 'slug',
            'terms'    => $specialty,
        )
    ),
    'meta_key'  => 'city',
    'meta_value'=> $location,
);

Leave a Reply

Your email address will not be published. Required fields are marked *