Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

get_terms with 3 levels... by ID´s WordPress

  • SOLVED

Hi,

I have tried to pass on an array to the


'child_of' => $parentids,


I have a 3 level custom taxonomy called location, and i am trying to make a query with get_terms where i could chose lets say 4-5 differnt id´s and then list the child of these selected id´s

$terms = get_terms($taxonomies, $args);

if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '' . $term->count . '</li>';

}
echo '</ul>';
}


Is this possible with Wordpress or would it requiere to do a custom walker for categories.... I would prefer to use the get_terms.

The id´s we pick is from second level and the granchildren are in 3. level...

in our case we would like to list taxonomy term with "254, 345, 234, 234" and their children....

Look forward to see if i am heading in the wrong direction.

Thanks in advance

Regards

René

Answers (2)

2015-10-02

Rempty answers:

Hi,
Parameter "child_of " use interger values not arrays, you need to call get_terms per each parent _id


$taxonomies='location';

/*Parent_id 254*/
$terms = get_terms($taxonomies, array('child_of'=>254));
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '' . $term->count . '</li>';

}
echo '</ul>';
}

/*Parent_id 345*/
$terms = get_terms($taxonomies, array('child_of'=>345));
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '' . $term->count . '</li>';

}
echo '</ul>';
}

//Repeat the same for the others



reneberg comments:

Hi,

Yes i was afraid that i had to do this... is it not possible in one query... I mean if i have to make 8 different queries... instead of one, thinking of performance. This is meant for a location taxonomy with 3 levels and thousands of cities.

Thanks


Rempty comments:

i created a custom query

<?php
$taxonomies=array('location');
$taxonomy='location';
$parents=array(294,254,300,295,296);

$cquery = "SELECT t.term_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('" . implode("', '",$taxonomies) . "') AND tt.parent IN ('" . implode("', '",$parents)."')";
$myterms = $wpdb->get_results($cquery);
echo '<ul>';
foreach ($myterms as $termid){
$term=get_term($termid->term_id,$taxonomy);

echo '<li><a href="'.get_term_link($term->term_id,$taxonomy).'">'.$term->name.'</a></li>';
}
echo '</ul>';
?>


reneberg comments:

Hi Rempty,

This works, now i only need to print out 10 in each and order by "count" and "DESC" and the get the title for each list (parent title). If you can help with that i will mark the thread as fixed.

Thanks

René


reneberg comments:

If you are able to help with that i will pay 20 dollars. (Dont know if it is possible to raise the bid)?


reneberg comments:

Here you have an image of what i try to do