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
<?php
$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>';
}
}
}
?>