[resolved] get all of the terms that exist in all taxonomies (2 posts)

  1. gpspake
    Posted 3 years ago #

    I may be going about this all wrong but I'm trying to use get terms to get all of the terms that exist in every taxonomy.

    I think this might be a php question.

    get_terms allows you to specify which taxonomies it gets terms from in array like this...
    get_terms( array(post_tag, category, nav_menu, link_category, post_format), $args )

    That works, but I'm writing a plugin and if people have custom taxonomies manually entering the array will not work.
    So I figured the solution would be to use get taxonomies to generate that array. The problem is, it comes back as an associatave array and I need a simple array. So I did this...

    $myarray = 'array('. implode(', ',get_taxonomies('','names')) .')';

    which, if I echo $myarray, returns:
    array(category, post_tag, nav_menu, link_category, post_format)

    perfect; That's exactly what I have manually entered. now I should be able to stick $myarray in get_get terms where I have it manually entered and it should get everything.
    Nope. It doesn't like it.

    Here's the entire snippet of code, along with a pic of what get's echoed out. You can see that $myarray is exactly the same as what's in the working get_terms but it's breaking the second get_terms.

    $myarray = 'array('. implode(', ',get_taxonomies('','names')) .')';
    echo $myarray . '<br/>';
    echo 'array(category, post_tag, nav_menu, link_category, post_format) <br/><br/>';
    print_r(get_terms( array(post_tag, category, nav_menu, link_category, post_format), $args )); echo '<br /><br />';
    print_r(get_terms( $myarray , $args )); echo '<br /><br />';


  2. gpspake
    Posted 3 years ago #

    Ok, so I don't know what I was doing wrong because I tried this before and couldn't get it to work, but I ended up using array_values() after a couple of people recommended it in another thread.

    I finally ended up writing it like this:

    $mytermobjects = get_terms( array_values((get_taxonomies('','names'))) , $args );

Topic Closed

This topic has been closed to new replies.

About this Topic