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!

  • this maybe? Except that the where condition changes a little to include the "term_id" of other term Commented Nov 12, 2012 at 18:17

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.


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

    $args = array(

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


Sorry for the long delay in replying, but thanks for your help guys, it's good reference!

vtxyzzy from wordpress.org forum help me with the query part, the ajax and php did myself, don't know if it's neat. Here is how the final code became in functions.php:

function restaurant_search( $taxonomy ) {
    <script type="text/javascript">
        jQuery(document).ready(function() {
            var cidade=jQuery('#cidade').val();
                    url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",
                    data:'action=category_select_action&name=' + cidade,

<form action="<?php echo home_url('/'); ?>" role="search" method="get">
    <?php // First dropdown
        $term_selected_cidade = get_terms('cidade');
        echo '<select id="cidade" name="cidade">';
        echo '<option disabled="disabled" selected="selected">Cidade</option>';
        foreach ($term_selected_cidade as $term) {
            echo '<option id=' . $term->term_id . ' value=' . $term->slug . '>' . $term->name . '</option>';
        echo '</select>';


    <!-- Second Dropdown -->
    <select name="cozinha" id="cozinha" >
        <option>Escolha uma cidade</option>
    <input type="submit" value="buscar" />

<?php }

function implement_ajax() {
    // Populates the second dropdown with cuisine terms
    global $wpdb;
    $cozinha_term = $_POST['name'];
    $query = "
        SELECT DISTINCT $wpdb->terms.name, $wpdb->terms.slug
        FROM $wpdb->terms
        INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id
        INNER JOIN $wpdb->term_relationships ON $wpdb->term_relationships.term_taxonomy_id=$wpdb->term_taxonomy.term_taxonomy_id
        LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->term_relationships.object_id
        WHERE $wpdb->term_taxonomy.taxonomy='cozinha' AND $wpdb->posts.ID IN (
        SELECT $wpdb->posts.ID
        FROM $wpdb->terms
        INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id
        INNER JOIN $wpdb->term_relationships ON $wpdb->term_relationships.term_taxonomy_id=$wpdb->term_taxonomy.term_taxonomy_id
        LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->term_relationships.object_id
        WHERE $wpdb->term_taxonomy.taxonomy='cidade' AND $wpdb->terms.slug='$cozinha_term')";

    $object = $wpdb->get_results($query);
    $cuisine_terms = array();
    for ($i = 0; $i < count($object); $i++) {
        $cuisine_terms[] = get_object_vars($object[$i]);

    echo '<select id="cozinha">' . '<option disabled="disabled" selected="selected">Cozinha</option>';
    foreach ($cuisine_terms as $term) {
        echo '<option value=' . $term['slug'] . '>' . $term['name'] . '</option>';
    echo '</select>';

add_action('wp_ajax_category_select_action', 'implement_ajax');
add_action('wp_ajax_nopriv_category_select_action', 'implement_ajax');

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.