WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm creating an age select menu in the admin, populated from a taxonomy of age. The taxonomy is hierarchical as follows:

  • 18-25 (parent, ID 183)
    • 18 (child)
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 26-30 (parent, ID 184)
    • 26
    • 27
    • 28
    • 29
    • 30

I would like to only list the children (18, 19 etc) and not the parents (18-25, 26-30) etc. Currently I am using get_terms with the parent argument, but it doesn't accept more than 1 parent ID. Here's what I have so far, which shows the children from 18-25.

    $ages = get_terms( 'age', array(
        'hide_empty' => 0,
        'parent' => '183',
    ));

Here's what I want it to do, but isn't supported. I have also tried it with an array but it doesn't work either.

    $ages = get_terms( 'age', array(
        'hide_empty' => 0,
        'parent' => '183,184',
    ));

I see there is a get_term_children function but I'm unsure of how to use this either as it looks like it only accepts one value also. Eg: In this example it would build an unordered list but I could modify for select menu.

<?php
    $termID = 183;
    $taxonomyName = "age";
    $termchildren = get_term_children( $termID, $taxonomyName );

    echo '<ul>';
    foreach ($termchildren as $child) {
    $term = get_term_by( 'id', $child, $taxonomyName );
    echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
    }
    echo '</ul>';
?> 
share|improve this question
    
Call get_terms twice and merge the two arrays of results? – Mark Duncan Jul 24 '11 at 15:21
    
@Mark Thanks, I thought of doing it this way, but even if I merge the ID's into an array, I can't see how it would work because it would be the same as listing them manually - 183, 184, which doesn't work. – Andrew Jul 24 '11 at 23:43
    
Having read the accepted answer i now realise your question wasn't entirely clear, from the looks of things you wanted all terms, excluding top level ones.. (which you can do with a single get_terms call). Your question read as if you were wanting all children of 2 particular parent terms.. – Mark Duncan Jul 26 '11 at 16:03
up vote 8 down vote accepted

This should work for you:

$taxonomyName = "age";
//This gets top layer terms only.  This is done by setting parent to 0.  
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false));   
echo '<ul>';
foreach ($parent_terms as $pterm) {
    //Get the Child terms
    $terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false));
    foreach ($terms as $term) {
        echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';  
    }
}
echo '</ul>';
share|improve this answer
    
Thanks @Manny, worked a treat. I took what you provided and mashed it into the select menu I was after. Great stuff. – Andrew Jul 25 '11 at 4:57
    
No problem. Glad it worked out for you. – Manny Fleurmond Jul 25 '11 at 5:21

If you display multiple parent's child, you can try this. Display mention term ids child term.

$termIds = array(367, 366, 365, 364, 363, 362);
$taxonomyName = "age";

$args = array(
    'orderby'           => 'term_id', 
    'order'             => 'DESC',                      
    'hide_empty'        => false,                       
    'childless'         => false,
); 

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

if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
$inc = 1;
foreach ( $terms as $term ) {                           
 if (in_array($term->parent, $termIds)) {
  echo '<option value="'.$term->term_id.'"><font><font>'.$term->name.'</font></font></option>';
    }
  }
}
share|improve this answer
    
Please explain why you think this code should work. Also, I am pretty sure that a hard-coded solution is not the best way. – s_ha_dum Dec 29 '15 at 14:23
    
Question mentions with ids, for this reason I have answered related think. – Jakir Hossain Dec 30 '15 at 15:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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