Dropdown: Display terms from B only if has relationship with a term A selected

I’ve googling for a solution for this issue and found nothing: I have some posts from a Custom Post Type (Restaurants) and its taxonomies (‘city’ and ‘cuisine’). Whenever I create a restaurant post, I can set many ‘cuisine’ terms as I want, but ‘city’ is single selected. I’ve also made a custom search using a html dropdown which I can search from ‘restaurants’ posts by filtering from its taxonomy terms.

The search works well but what I really want is populate the second taxonomy (cuisine) only with terms that has some relationship based on the first taxonomy chosen (city). In the first dropdown, if someone choose the city ‘New York’ the second dropdown must display only cuisine terms that has relationship with ‘New York’ city term on restaurant posts. If there’s not a post with ‘brazilian’ cuisine and ‘New York’ city registered, the ‘brazilian’ term must not appear on second dropdown. Of course, the cuisine dropdown doesn’t have to display duplicated terms.

Is that possible? Thanks in advance!

2 Answers
2

Here’s part of the answer… how to query for the terms you’re looking for. But it sounds like you’ll need some ajax to re-query for each selection.

City and cuisine would be dynamic, but essentially the when the user selects the city populate that data into the $city variable, and so on.

Someone else will need to chime in with the ajax part.

<?php

    $city = array( 'dallas' );
    $cuisine = array( 'italian', 'mexican', 'french );

    $args = array(

        'post_type' => 'restaurant',
        'posts_per_page' => -1,
        'tax_query' => array(
        'relation' => 'AND',
             array(
                'taxonomy' => 'city',
                'field' => 'slug',
                'terms' => $city
            ),
            array(
                'taxonomy' => 'cuisine',
                'field' => 'slug',
                'terms' => $cuisine
            )
        )
    );

?>

Leave a Comment