2

Is there an easy way of obtaining every registered taxonomy and for each registered taxonomy, get all the terms for that taxonomy, and for each term, get the post count, without actually fetching all of the post data?

I would assume it is most definitely possible. I would also assume it requires some massively long database query using $wpdb.

2 Answers 2

8

You can do this with just get_terms - this allows you to fetch all (or some) of the terms from one (or more) taxonomies.

By default it excludes 'empty' terms, so you'll need to set the arguments appropriately.

 //Array of taxonomies to get terms for
 $taxonomies = array('category','post_tags','my-tax');
 //Set arguments - don't 'hide' empty terms.
 $args = array(
     'hide_empty' => 0
 );

 $terms = get_terms( $taxonomies, $args);
 $empty_terms=array();

 foreach( $terms as $term ){
     if( 0 == $term->count )
          $empty_terms[] = $term;

 }

 //$empty_terms contains terms which are empty.

If you wish to obtain an array of registered taxonomies programmatically you can use get_taxonomies()

2
<?php
// your taxonomy name
$tax = 'cat';

// get the terms of taxonomy
$terms = get_terms( $tax, $args = array(
  'hide_empty' => false, // do not hide empty terms
));

// loop through all terms
foreach( $terms as $term ) {

    // Get the term link
    $term_link = get_term_link( $term );

    if( $term->count > 0 )
        // display link to term archive
        echo '<a href="' . esc_url( $term_link ) . '">' . $term->name .'</a>';

    elseif( $term->count !== 0 )
        // display name
        echo '' . $term->name .'';
}
?>

Your Answer

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

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