0

I want to achieve 3 post display from each taxonomy terms. But this terms will be passed from lowest of hierarchy in array that is ('munnar','cochin') etc.

Europe
Asia
- India
-- Kerala
--- Munnar
--- Thekkady
--- Cochin

I was using get_ancestors(); in order to get parent list of above passed grand-child terms.

But somehow I am not getting post list of "Munnar" as well as "Cochin", only getting list of Thekkady, Kerala, India.

I want to get list of Munnar + Cochin + their all Parent Post list...

Please check my code for what wrong I am doing in:

$tax = 'selcountrie';
$taxo_terms = array('munnar','cochin');
$locations = get_the_terms( get_the_ID(), 'selcountrie' );
$pat = '<a href="'.site_url().'/location/%s">%s</a><br>';
foreach ( $locations as $location ) {
$childposts = new WP_Query(
  array(
    'post_type' => 'post',
    'posts_per_page' => 3,
    'tax_query' => array(
        array(
            'taxonomy' => 'selcountrie',
            'field'    => 'term_id',
            'terms'    => $location->term_id
        ),
    ),
  )
);
printf($pat, $location->slug,$location->name);
foreach($childposts->posts as $cp){
    echo '<li><a href="'.get_permalink($cp->ID).'">'.$cp->post_title.'</a></li>';
}
$anc = get_ancestors($location->term_id,'selcountrie');
  if (!empty($anc)) {
    foreach ($anc as $term) {
        $t = get_term_by( 'term_id', $term, 'selcountrie');
        //echo "<pre>";print_r($t); "</pre>";
        echo "<br>";
        printf($pat,$t->slug,$t->name);
        $parentposts = new WP_Query(
          array(
            'post_type' => 'post',
            'posts_per_page' => 3,
            'tax_query' => array(
                array(
                    'taxonomy' => 'selcountrie',
                    'field'    => 'term_id',
                    'terms'    => $t->term_id,
                    'include_children' => false
                ),
            ),
          )
        );
        foreach($parentposts->posts as $pp){
            echo '<li><a href="'.get_permalink($pp->ID).'">'.$pp->post_title.'</a></li>';
        }
    }
  }
  break;
}

Above code shows below output:

output I get

Sorry as I am very new to this.

Thanks for Help in Advance !!

Your Answer

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

Browse other questions tagged or ask your own question.