How to filter the taxonomy terms based on another taxonomy term

I have created a custom Post Type called “Tour” which Holds up-to five Taxonomies called “destination”, “Types”, “Month of Travel”, “Year of Travel” and “Led by”.

Now I brought them as drop-down category using “wp_dropdown_categories” – (each of the taxonomy were listed as drop down). and search term works fine in the combinations (if exist).

Read More

for example:: I am searching for “destination” as “Bangalore” and “Month of travel” as “June” combination works as post were chosen on particular terms. ~ it’s perfect.

But, when there is no post chosen for a category / no search combination avail… It triggers “No post available”. ~ its a common things in word press.

So I prefer to filter the TAXONOMIES based on the Previous TAXONOMIES (Its little confusing while relating them).

Here is the main concept:

If I choose the “destination” as “Bangalore” I need the “Type” to be filtered based on the destination. that is Bangalore can have only two types “A type and B Type” rest of the type should be ignored. – (Once again all the five thing comes as Taxonomies).

I want something like thisenter image description here

if post_type=tour&tour_destination=bangalore filter the other taxonomies with relevant terms?

Help me ~ thanks in advance

dev

Related posts

Leave a Reply

1 comment

  1. I did few tweaks on the earlier code, the first filter is working fine for now, I am little confused on the rest any suggestion sven ? can you help me to bring this effectively.

    <!-- last edit started by dev -->
     <script type="text/javascript">
      function onchangedestination(str)
        {
          var xmlhttp;
    
          if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
          }
          else
          {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          } 
    
          xmlhttp.onreadystatechange = function() {
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
              document.getElementById("data").innerHTML = xmlhttp.responseText;
            }
          }
    
          xmlhttp.open("GET","http://example.com/ajax?opt="+str, true);
          xmlhttp.send();
        }
    </script>
     <select class="tourdestination" name="tourdestination" id="tourdestination" onchange="onchangedestination(this.value);">
     <option value="" selected="selected">All Destinations</option>
        <?php
        foreach( get_categories('taxonomy=tour_destination&post_type=tour&hide_empty=false') as $cat ) :
    
        if( !$cat->parent ) // if cat info not has parents
        {
            echo '<option value="' . $cat->term_id  . '" >' . $cat->name . '</option>';
            category_tree( $cat->term_id ); 
        }
        endforeach;
    
        wp_reset_query();
    
        function category_tree( $cat ) { // passing ids of category
            $args = array('category__in' => array( $cat ));
            $next = get_categories('taxonomy=tour_destination&hide_empty=false&parent=' . $cat);
    
            if( $next ) :
                foreach( $next as $cat ) :
                    echo '<option value="' . $cat->term_id  . '" >&nbsp;' . $cat->name.'</option>';
                    category_tree( $cat->term_id );
                endforeach;
            endif;
        }
        ?>
     </select>
    
    <div id="data"></div>
    

    file test.php

    <?php
      $opt = $_GET['opt'];
      $pages = get_posts(array(
                     'post_type' => 'tour',
                     'numberposts' => -1,
                     'tax_query' => array(
                        array
                        (
                          'taxonomy' => 'tour_destination',
                          'field' => 'id',
                          'terms' => $opt, // above code will return the id here.
                          'include_children' => false
                        )
                        )
                        ));
    
    
        $post_id =array();
        //$tour_type = array();
        $categories = array();
        foreach ($pages as $page) 
            { 
                $post_id[] = $page->ID;
            }
    ?>
     <?php //echo $count = count($post_id) . 'Total Posts <br>'; ?>
     <?php foreach($post_id as $p_ID): ?> 
     <?php $tours = wp_get_post_terms($p_ID, 'tour_type', array("fields" => "all")); ?> 
     <?php $month_travels = wp_get_post_terms($p_ID, 'month_travel', array("fields" => "all")); ?> 
     <?php $year_travels = wp_get_post_terms($p_ID, 'year_travel', array("fields" => "all")); ?>
     <?php $led_bys = wp_get_post_terms($p_ID, 'led_by', array("fields" => "all")); ?>
    
     <!-- fetching tour data -->
     <?php foreach($tours as $tour):  ?>
     <?php $array_tour['id'] = $tour->term_id; ?>
     <?php $array_tour['name'] = $tour->name; ?>
     <?php endforeach; ?>
    
     <!-- fetching month data -->
     <?php foreach($month_travels as $month_travel):  ?>
     <?php $array_month['id'] = $month_travel->term_id; ?>
     <?php $array_month['name'] = $month_travel->name; ?>
     <?php endforeach; ?>
    
     <!-- fetching year data -->
     <?php foreach($year_travels as $year_travel):  ?>
     <?php $array_year['id'] = $year_travel->term_id; ?>
     <?php $array_year['name'] = $year_travel->name; ?>
     <?php endforeach; ?>
    
     <!-- fetching led by data -->
     <?php foreach($led_bys as $led_by):  ?>
     <?php $array_led_by['id'] = $led_by->term_id; ?>
     <?php $array_led_by['name'] = $led_by->name; ?>
     <?php endforeach; ?>
    
     <?php $categories_tour[] = array('id' => $array_tour['id'], 'name' => $array_tour['name']); ?> 
     <?php $categories_month[] = array('id' => $array_month['id'], 'name' => $array_month['name']); ?> 
     <?php $categories_year[] = array('id' => $array_year['id'], 'name' => $array_year['name']); ?> 
     <?php $categories_led_by[] = array('id' => $array_led_by['id'], 'name' => $array_led_by['name']); ?> 
    
     <?php endforeach; ?>
    
     <!--Removing duplicates-->
     <?php $input_tour = array_map("unserialize", array_unique(array_map("serialize", $categories_tour))); ?>
     <?php $input_month = array_map("unserialize", array_unique(array_map("serialize", $categories_month))); ?>
     <?php $input_year = array_map("unserialize", array_unique(array_map("serialize", $categories_year))); ?>
     <?php $input_led_by = array_map("unserialize", array_unique(array_map("serialize", $categories_led_by))); ?>
    
     <select id="type">
     <?php foreach($input_tour as $tour_category): ?>
     <?php echo "<option id=" . $tour_category['id'] .">" . $tour_category['name'] . "</option>"; ?>
     <?php endforeach; ?>
     </select>
    
     <select id="month">
     <?php foreach($input_month as $month_category): ?>
     <?php echo "<option id=" . $month_category['id'] .">" . $month_category['name'] . "</option>"; ?>
     <?php endforeach; ?>
     </select>
    
     <select id="year">
     <?php foreach($input_year as $year_category): ?>
     <?php echo "<option id=" . $year_category['id'] .">" . $year_category['name'] . "</option>"; ?>
     <?php endforeach; ?>
     </select>
    
     <select id="led_by">
     <?php foreach($input_led_by as $led_by_category): ?>
     <?php echo "<option id=" . $led_by_category['id'] .">" . $led_by_category['name'] . "</option>"; ?>
     <?php endforeach; ?>
     </select>
    

    If I make mistake any where please advice me …, Thanks