1

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).

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

  • To filter those taxonomies, have you kept any relation between them? Like if I choose Bangalore and I have to filter types taxonomy, than there should be some relation to filter it?? – sven Mar 22 '14 at 6:34
  • This might be helpful for you wordpress.org/plugins/multiple-category-selection-widget – sven Mar 22 '14 at 6:46
  • thanks for replying.. is that possible to relate them using the post (i.e.) when selecting the particular terms say 'Bangalore' it should retrieve all post related to particular term Bangalore and in such case I can retrieve other terms from that for eg: type will be 8 days and 7 days, I am looking to relate them using the post chosen for the term,... – Dev Mar 22 '14 at 6:47
  • let me try with the plugin @sven – Dev Mar 22 '14 at 6:49
  • nope plugin brings only wp_dropdown_category functionality however I want to filter them one by one – Dev Mar 22 '14 at 7:07
0

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

  • where did you get ' opt in the '$opt = $_GET['opt'];? – bdtheme Nov 19 '16 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.