I'm trying to get the parent level and the first child level under that. I tried using get_terms for this but I was unable I'm guessing I need something more extensive.

I know I can use get terms running in a foreach but that's a lot of queries to run on page load so I'm hoping for a simpler solution. Any thoughts?

  • Do you want a list of all your taxonomies and with a list of all the terms belonging to that taxonomies? I don't think that is possible in one query. You will need to run a couple of queries here. Jun 19, 2014 at 5:51
  • I ended up using multiple queries. I just need parent and first child level. So I run one query to get all the parents and then in a forever and look for all taxonomy with that parent. So I'm running 10-15 queries which seems like a lot but page load still seems ok so I went with it. Jun 19, 2014 at 14:25

1 Answer 1


There is unfortunately no way that you are going to do this in one or two queries. It seems that there are a misunderstanding or misconception about database queries. The most important aspect to keep in mind when you are running database queries is the amount of database queries vs. the time these queries will take.

It is no use running one or two queries that takes more time than what 10 queries would have taken you to do the exact same job. BTW, functions like get_terms and get_taxonomies are very inexpensive, so even if you make 20 database queries, it will be lightning fast, so you have no need to worry here.

You will have to use two functions here to list your taxonomies and the terms belonging to these taxonomies. The first function will be get_taxonomies() which will get a list of all your taxonomies. The most important argument to use here is _builtin, which must be set to false. The reason being is that categories, tags and even post formats are also taxonomies, so you will need to exclude these.

Secondly you need to use the get_terms function to get all the terms belonging to these taxonomies. You will need to return the taxonomy name from the foreach loop as $taxonomies to get the terms belonging to that specific taxonomy.

Here is the complete code

$args = array(
    'public'   => true,
    '_builtin' => false

$output = 'names'; // or objects
$operator = 'and'; // 'and' or 'or'
$taxonomies = get_taxonomies( $args, $output, $operator ); 

if ( $taxonomies ) {
    foreach ( $taxonomies  as $taxonomy ) {
        echo '<p>' . $taxonomy . '</p>';

        $terms = get_terms($taxonomy);
        if ( !empty( $terms ) && !is_wp_error( $terms ) ){
            echo '<ul>';
            foreach ( $terms as $term ) {
                echo '<li>' . $term->name . '</li>';
            echo '</ul>';
  • This is pretty much exactly what I ended up doing except I used get_terms instead. That's good to know about them not being expensive resource-wise. Thanks a ton for the input! Jun 19, 2014 at 18:09
  • My pleasure. Glad I could help Jun 19, 2014 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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