WordPress.org

Support

Support » How-To and Troubleshooting » get_the_terms using array of taxonomies

get_the_terms using array of taxonomies

  • What am I missing?
    The codex clearly states that the 2nd argument to the the function get_the_terms can be either a string or array.

    But I get unpredictable results in $termarray when trying to do something like this:

    $id = 12;
    $taxonomy = array('people','places');
    $termarray = get_the_terms($id, $taxonomy);

    And I haven’t been able to find an example of code that works using the array option. Can someone show me a working example of this syntax, and maybe add it to the codex?

Viewing 11 replies - 1 through 11 (of 11 total)
  • This seems to work for me. Can you describe how it is not working for you? What results do you get?

    This issue read it head again.

    Breaking it down even further using this example:

    //$taxarray[] = '';
    //$taxarray = array();
    //$taxarray[] = 'cuisine';
    //$taxarray = array ('ingredient','cuisine');
    //$taxarray = array ('ingredient');
    //$taxarray = 'ingredient';
    $taxarray = 'cuisine';
    
    $terms = get_the_terms($pid,$taxarray);

    The only variations that works properly are the last two, simply assigning a single string to the 2nd argument.

    Using any of the previous formats (an array of strings or an empty array) give the same wrong results which are some terms that are partly in and partly out of the taxonomies in the array. That’s what I mean by “unpredictable” … the result are not related to input.

    For proof that $taxarray really is being used, assigning this $taxarray = ”; gives no results … as expected.

    So it seems that an array of strings is the incorrect format and the inefficient workaround I came up with is to loop through the taxarray, calling get_the_terms for each taxonomy and appending the resulting $terms to a yet another array.

    Are there any working examples of how to use an array of taxonomies as the 2nd argument to get_the_terms() ?

    If you do

    $pid = 12;
    $taxarray = array ('ingredient','cuisine');
    $terms = get_the_terms($pid,$taxarray);

    and print_r $terms what do you actually get?

    Does it return a wordpress error? maybe ingredient is not the correct taxonomy name (typo error or something)

    executing this:

    $taxarray = array ('ingredient','cuisine');
    $terms = get_the_terms($pid,$taxarray);
    print_r($terms);

    I get this (no errors):

    Array ( 
    
    [0] => stdClass Object (
    [term_id] => 2053
    [name] => dairy-free
    [slug] => dairy-free
    [term_group] => 0
    [term_order] => 0
    [term_taxonomy_id] => 2167
    [taxonomy] => diet
    [description] =>
    [parent] => 0
    [count] => 5134 ) 
    
    [1] => stdClass Object (
    [term_id] => 2056
    [name] => gluten free
    [slug] => gluten-free
    [term_group] => 0
    [term_order] => 0
    [term_taxonomy_id] => 2170
    [taxonomy] => diet
    [description] =>
    [parent] => 0
    [count] => 8469 ) 
    
    [2] => stdClass Object (
    [term_id] => 2035
    [name] => mexican
    [slug] => mexican
    [term_group] => 0
    [term_order] => 0
    [term_taxonomy_id] => 2149
    [taxonomy] => cuisine
    [description] =>
    [parent] => 2011
    [count] => 411 ) 
    
    [3] => stdClass Object (
    [term_id] => 2228
    [name] => seafood
    [slug] => seafood
    [term_group] => 0
    [term_order] => 0
    [term_taxonomy_id] => 2343
    [taxonomy] => dishtype
    [description] =>
    [parent] => 0
    [count] => 1248 ) 
    
    [4] => stdClass Object (
    [term_id] => 2086
    [name] => whole30
    [slug] => whole30
    [term_group] => 0
    [term_order] => 0
    [term_taxonomy_id] => 2200
    [taxonomy] => diet
    [description] =>
    [parent] => 0
    [count] => 2964 )
    
    )

    Notice that it has terms in cuisine, and other taxonomies that were not asked for but never ingredient.

    Also, some of the returned terms are from hierarchical taxonomies and some are not so that is not the issue.

    I’m sure ingredient is spelled right and the $pid is valid.

    Bizarre as it seems, executing this (with different, but valid taxonomy name values) gives the exact same results for that $pid (different results on a different $pid) … so where it the world is it getting the $taxarray values from?!

    $taxarray = array ('diet','dishtype');
    $terms = get_the_terms($pid,$taxarray);
    print_r($terms);

    2 things – 1, has post 12 for sure got any ingredient terms associated to it?

    $pid = 12;
    $terms = get_the_terms($pid, ‘ingredient’);
    print_r($terms);

    The second issue is a bit weird, the docs do say you can pass an array as the second parameter, but it’s acting like it’s null (retrieving all taxonomies) if you do.

    You might have to do something like

    $taxonomies = array ('ingredient','cuisine');
      $pid = 12
      foreach ( $taxonomies as $taxonomy ){
    		$terms = get_the_terms($pid, $taxonomy );
    		DO SOMETHING WITH $terms;
      }

    It’s not very heavy don’t worry about it.

    Lee.

    Yes, $pid = 12 has terms in the tax ‘ingredient’

    Here is the result of:

    $terms = get_the_terms($pid,'ingredient');
    print_r($terms);
    Array ( 
    
    [2285] => stdClass Object (
    [term_id] => 2285
    [name] => avocado
    [slug] => avocado
    [term_group] => 0
    [term_order] => 0
    [term_taxonomy_id] => 2400
    [taxonomy] => ingredient
    [description] =>
    [parent] => 0
    [count] => 486
    [object_id] => 81085 ) 
    
    [2286] => stdClass Object (
    [term_id] => 2286
    [name] => bacon
    [slug] => bacon
    [term_group] => 0
    [term_order] => 0
    [term_taxonomy_id] => 2401
    [taxonomy] => ingredient
    [description] =>
    [parent] => 2610
    [count] => 733
    [object_id] => 81085 ) 
    
    [2610] => stdClass Object (
    [term_id] => 2610
    [name] => pork
    [slug] => pork
    [term_group] => 0
    [term_order] => 0
    [term_taxonomy_id] => 2728
    [taxonomy] => ingredient
    [description] =>
    [parent] => 0
    [count] => 1561
    [object_id] => 81085 ) 
    
    [2622] => stdClass Object (
    [term_id] => 2622
    [name] => prawns
    [slug] => prawns
    [term_group] => 0
    [term_order] => 0
    [term_taxonomy_id] => 2740
    [taxonomy] => ingredient
    [description] =>
    [parent] => 0
    [count] => 308
    [object_id] => 81085 )
    
    )

    Interestingly, the array structure resulting from using the simple string argument is one level less deep.

    My workaround (2 months ago, and again now) is exactly what you suggested. I put get_the_terms inside a loop for each taxonomy name and reassembled the results.

    Don’t worry about it, it may be a wp bug so I’m moving on 🙂

    Thanks for your efforts, Ron

    Hi Ron,

    I’ve just tested it on a local build and it works 100% as expected for me.

    $taxes = array('product_cat');
    $terms = get_the_terms($post->ID, $taxes);

    gives me product_cat terms

    $taxes = array('product_cat', 'tile_effects');
    $terms = get_the_terms($post->ID, $taxes);

    gives me ‘product_cat’ and ’tile_effects’ terms.

    Must be something in your setup, have you tried a new array name, make sure it’s not getting globally filled somehow?

    The only other thing to mention is that I am using the name I used to register the taxonomies. so…

    register_taxonomy(‘THIS_NAME’,$x,$y);

    Lee.

    Well I guess that’s good to know that it works for someone.

    I’m using the name it is registered as.

    Using a new unused array var like $ta makes no difference.

    My register_taxonomy args are this

    $args = array (
      'labels' => $labels, // defined above
      'public' => true, //defaults to true
      'show_ui' => true, //defaults to public
      'show_in_nav_menus' => true,  //defaults to public
      'show_tagcloud'  => true, //defaults to show_ui
      'show_admin_column' => false, // defaults to false
      'hierarchical' => true, //defaults to false
      'query_var' => true, // default is true which is the taxonomy name
    );
    register_taxonomy('ingredient', array('recipe','ad','post','page'), $args);

    At this point it is archeology so maybe someone who has the same symptoms will dig this up and discover that is was simply a major case of dumbass with an “obvious” error. 🙂

    Maybe it is something outside of wordpress like some php or mysql config that says “do weird stuff with array arguments”.

    yeah not sure. You could try this, check all is well…

    Change ‘page’ for whatever post type id=12 is.

    $tax = get_object_taxonomies( ‘page’, $output );
    print_r($tax);

    one item in the array should be keyed ‘ingredients’

    hi
    <?php
    $args = array(
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘products’,
    //this is the taxonomy in function we use that: //register_taxonomy(‘products-//tags’,’products‘,$args_tags);

    ‘field’ => ‘slug’,
    ‘terms’ => ‘ram’ // the slug name we used
    )
    )
    );
    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo ‘

  • ‘ . get_the_title() . ‘
  • ‘;
    }
    } else {
    echo ‘no result found’;
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    ?>

    `

hi
<?php
$args = array(
‘tax_query’ => array(
array(
‘taxonomy’ => ‘products’,

//*this is the taxonomy in function we use that:register_taxonomy(‘products-tags’,’products‘,$args_tags);
*//

‘field’ => ‘slug’,
‘terms’ => ‘ram’ // the slug name we used
)
)
);
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo ‘

  • ‘ . get_the_title() . ‘
  • ‘;
    }
    } else {
    echo ‘no result found’;
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    ?>

    `

    Viewing 11 replies - 1 through 11 (of 11 total)
    • The topic ‘get_the_terms using array of taxonomies’ is closed to new replies.
    Skip to toolbar