1

I do have a custom taxonomy called "countries". How do I get each term (country) with the number of its published posts in brackets, like the following:

  • Uruguay (3)
  • Chile (5)
  • Thailand (2)
  • etc.

With following code the number of all terms in the "countries" taxonomy is displayed:

$countries_count = wp_count_terms( 'countries' );
echo $countries_count; 

But I just know that this is just a starting point of my problem. Any suggestions?

  • I can't test this right now but try getting all the terms for "country" and then loop through them and get the wp_count_terms for each of them. `$terms = get_terms("countries"); if ( !empty( $terms ) && !is_wp_error( $terms ) ){ echo "<ul>"; foreach ( $terms as $term ) { $args = array( 'slug' => $term->slug, ); echo "<li>" . $term->name . "(" . wp_count_terms('countries', $args) . ")</li>"; } echo "</ul>"; }' – leromt Jun 5 '14 at 21:05
  • @leromt : Please don't post large blocks of code in the comments. If you think you have an answer, post it in the answer box. – s_ha_dum Jun 5 '14 at 21:26
  • @s_ha_dum: Sorry, will do. – leromt Jun 5 '14 at 21:27
2

You can use get_terms to get the list of all terms associated with a taxonomy. Once you have all the separate terms, you can use $term->name to display the name of the term and $term->count to retrieve the amount of posts inside that specific term.

Here is a slightly modified version of the code found in the codex. You can futher modify the output as you need

<?php  
$terms = get_terms('countries');

 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
     echo '<ul>';
     foreach ( $terms as $term ) {
       echo '<li>' . $term->name . '&nbsp;(' . $term->count . ')' . '</li>';
     }
     echo '</ul>';
 } 
?> 

EDIT

Thanks to @Traveler, here is another version of my code if you need the links to be clickable.

<?php 

  $terms = get_terms('countries'); 
  if ( !empty( $terms ) && !is_wp_error( $terms ) ){ 
  echo '<ul>'; 

  foreach ( $terms as $term ) { 
     $term = sanitize_term( $term, 'countries' ); 
     $term_link = get_term_link( $term, 'countries' ); 

      echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '&nbsp;(' . $term->count . ')' . '</a></li>'; 
  } 
  echo '</ul>';
  }

?> 
  • Thanks for the code, it works great! For all who want the above code also being clickable to the link of each term, use the following code: <?php $terms = get_terms('countries'); if ( !empty( $terms ) && !is_wp_error( $terms ) ){ echo '<ul>'; foreach ( $terms as $term ) { $term = sanitize_term( $term, 'countries' ); $term_link = get_term_link( $term, 'countries' ); echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '&nbsp;(' . $term->count . ')' . '</a></li>'; } echo '</ul>'; } ?> – Traveler Jun 10 '14 at 3:46
  • Just to add a little bit more in, if you include 'hide_empty=0' just after the term 'countries', you will be able to show all the results. So the first line will read: $terms = get_terms('countries', 'hide_empty=0'); – damienoneill2001 Apr 27 '16 at 11:11
0

You can try it with WP Query. I haven't tested it yet, so please let me know if it works.

$query = new WP_Query( array( 'taxonomy' => 'term', 'posts_per_page' => -1 ) );
$count = $query->post_count;
0

I can't test this right now but try getting all the terms for "countries" and then loop through them and get the wp_count_terms for each of them.

    $terms = get_terms("countries"); 
    if ( !empty( $terms ) && !is_wp_error( $terms ) ){
     echo "<ul>"; 
     foreach ( $terms as $term ) { 
     $args = array( 'slug' => $term->slug, ); 
     echo "<li>" . $term->name . "(" . wp_count_terms('countries', $args) . ")</li>";
    } 
    echo "</ul>"; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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