I'm building a villa rental website, I have two custom post types named "Villa" (used to show villas) and "Destination".
I have installed the CPT-onomies plugin and I use the "Destination" as custom taxonomy for the villas post type.

I want on each Destination post (ex. the post showing Greece as destination) to show the number of Villas (posts) assigned to that Destination (custom post type).

I have read this post but this code is only showing a list of all taxonomies and next to it a number showing the post count.

This is the code showing all the taxonomies and post count.

$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>';

Thank you for your help. Sorry but English is not my native language I tried to describe my problem best possible.


1 Answer 1


Normally usage of get_term() or get_term_by() is pretty much clear cut. See the basic example below:

// ID is needed
$term_by_id = get_term( 12345, 'destination' );
echo $term_by_id->count;
// Or if you e.g. want use the name (or slug or id or term_taxonomy_id)
$term_by_name = get_term_by( 'name', 'greece', 'destination' );
echo $term_by_name->count;

Note: I said normally, because I don't know anything about the plugin you are using.

  • Hi, thank you for your answer, instead of 'greece' can I add something like <?php echo get_the_title(); ?> to auto get the title? Thank you.
    – BigDropGR
    May 26, 2015 at 10:12
  • You could do $title = get_the_title(); and then $term_by_name = get_term_by( 'name', $title, 'destination' );. If that works with the plugin. @BigDropGR May 26, 2015 at 10:19
  • Unfortunately it doesn't work. Maybe the way the plugin registers the custom post type is the problem. I should find a way to register the custom post type "destination" as a custom taxonomy, from the code and not with the plugin. Thank you.
    – BigDropGR
    May 26, 2015 at 10:24
  • Did you take a look at cpt-onomies documentation, it seems to be pretty good. For get_term_by() you will see the class method has to be used. @BigDropGR May 26, 2015 at 12:50

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.