Query posts from a child taxonomy term id

this is my structure….

State1

Read More
CT1-1
     - Car_Model Post-CT1-1 (the posts of child)
     - Car_Model Post-CT1-2
CT2-1
     - Car_Model Post-CT2-2
CT3-1
     - Car_Model Post-CT3-1

State2

CT1-2
CT2-2
CT3-2

State3

CT1-3
CT2-3
CT3-3

Here is the logic of what i have done to display triple drop down for displaying 2 taxonomies and a child

  • 1st drop ——> State (Taxonomy)
  • 2nd drop ——> City (child of State Taxonomy)
  • 3rd drop ——> Manufacturer (Taxonomy)

now instead of manufacturer in the 3rd drop i have to Query posts from a taxonomy term id on selecting the 2nd drop

 <!-- WORKING LOGIC Triple Drop Down Start -->

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" ></script>
 <script type="text/javascript">
$(function()
{
    $('#main_cat').change(function()
    {
        var $mainCat=$('#main_cat').val();

        // call ajax
        $("#sub_cat").empty();
        $("#manu_cat").empty();
        $.ajax
        (
            {
                url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",     
                type:'POST',
                data:'action=my_special_ajax_call&main_catid=' + $mainCat,

                success:function(results)
                {
                    //  alert(results);
                    $("#sub_cat").removeAttr("disabled");       
                    $("#sub_cat").append(results);
                // it should be closed here an perform new ajax call not in success ok ? 
                    // Passing the sub_cat to the function for displaying 3rd drop down list
                    $(function()
                    {
                        $('#sub_cat').change(function()
                        {
                            var $subCat=$('#sub_cat').val();

                            // call ajax
                            $("#manu_cat").empty();
                            console.log($("#manu_cat"));
                            $.ajax
                            (
                                {
                                    url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",     
                                    type:'POST',
                                    data:'action=my_special_ajax_call&main_brand_id=' + $subCat,

                                    success:function(results)
                                    {
                                        //  alert(results);
                                        $("#manu_cat").empty();
                                        $("#manu_cat").removeAttr("disabled");      
                                        $("#manu_cat").append(results); 
                                    }
                                }
                            );                                    
                        });
                    }); // function end for manufature jQuery
                }
            }
        );                                    
    });
});     
// for manufacturer logic paste here

 </script>
 <style type="text/css">
 #content{width:auto; height:400px; margin:50px;}
 </style>

 <form role="search" method="get" id="searchform" action="<?php echo get_option('siteurl');?>/">
<div id="content">
    <?php 
        wp_dropdown_categories('show_count=0&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=1&show_option_none=Select State&name=main_cat&taxonomy=state');
    ?>
    <select name="sub_cat" id="sub_cat" disabled="disabled"></select>
    <select name="manu_cat" id="manu_cat" disabled="disabled"></select>

    <input type="submit" id="searchsubmit" value="Search" />        
</div>
</form> 


<!-- WORKING LOGIC Triple Drop Down End -->

Functions.php

function implement_ajax() {
if(isset($_POST['main_catid']))
        {
        $categories=  get_categories('child_of='.$_POST['main_catid'].'&hide_empty=0'.'&taxonomy=state');
//change the taxonomy state in your case
          foreach ($categories as $cat) {
            $option .= '<option value="'.$cat->term_id.'">';
            $option .= $cat->cat_name;
            //$option .= ' ('.$cat->category_count.')';
            $option .= '</option>';
          }

          echo '<option value="-1" selected="selected">Select City</option>'.$option;
        die();
        } // end if

if(isset($_POST['main_brand_id']))
        {
        $categories=  get_categories('&hide_empty=0'.'&taxonomy=manufacturer');
//change the taxonomy manufacturer in your case
          foreach ($categories as $cat) {
            $options .= '<option value="'.$cat->term_id.'">';
            $options .= $cat->cat_name;
            //$option .= ' ('.$cat->category_count.')';
            $options .= '</option>';
          }

          echo '<option value="-1" selected="selected">Select Make</option>'.$options;
        die();
        } // end if


}
add_action('wp_ajax_my_special_ajax_call', 'implement_ajax');
add_action('wp_ajax_nopriv_my_special_ajax_call', 'implement_ajax');//for users that are not logged in.

The logic starting from this line if(isset($_POST[‘main_brand_id’])) has to be replaced, so here if(isset($_POST[‘main_brand_id’])) i have to pass the term_id of the taxonomy to retrieve the post, kindly help how to query the logic to retrieve the relevant posts titles. Hope someone will help me……..

Related posts

Leave a Reply

1 comment

  1. i solved the problem

    if(isset($_POST['main_brand_id']))
            {
            $term_id .= $_POST['main_brand_id'];
            $term = get_term( $term_id, 'state' );
            $slug = $term->slug;
            global $post;
            $post_id = $post->ID;
            $args = array( 'post_type' =>'dealers','taxonomy'=>'state', 'term' => $slug );
            $myposts = get_posts( $args );
    
            foreach( $myposts as $post ) :    setup_postdata($post);
                echo '<option value="'.$post->ID.'">'
                    $options .=  the_title();
                echo '</option>';
            endforeach;
            die();
            } // end if
    

    the above piece of code displays the posts titles in drop down on selecting the child taxonomy terms. Go ahead feel free to use the code………..