So I'd like to get the top 5 post_tags by count. But I have an additional taxonomy that I use to further organize posts called Issue. So I want to be able to get the top 5 post tags used by posts in a certain taxonomy.

I can currently get the top 5 posts tags by count system wide thats not a problem.

$taxonomies = array( 

$args = array(
    'orderby'       => 'count', 
    'order'         => 'DESC',
    'hide_empty'    => true, 
    'exclude'       => array(), 
    'exclude_tree'  => array(), 
    'include'       => array(),
    'number'        => '5', 
    'fields'        => 'all', 
    'slug'          => '', 
    'parent'         => '',
    'hierarchical'  => true, 
    'child_of'      => 0, 
    'get'           => '', 
    'name__like'    => '',
    'pad_counts'    => false, 
    'offset'        => '', 
    'search'        => '', 
    'cache_domain'  => 'core'

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

$count = count($terms); $i=0;
if ($count > 0) {
    $cape_list = '<p class="my_term-archive">';
    foreach ($terms as $term) {
        $term_list .= '<a href="/term-base/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
        if ($count != $i) $term_list .= ' &middot; '; else $term_list .= '</p>';
    echo $term_list;

Thats the easy part. Any ideas on how to limit this to posts that are inside a specific taxonomy?

  • "Any ideas on how to limit this to posts that are inside a specific taxonomy?" - But you're not fetching posts here. You're just trying to get post_tags right? This would actually be easier if it were a wp_query object and you were getting posts, as you could use the tax_query parameters to poll multiple taxonomies.
    – GhostToast
    Commented Jun 25, 2013 at 16:49
  • Right I'm not actually fetching posts. I just want to get the post_tags for the posts that are inside a specific taxonomy. Commented Jun 25, 2013 at 16:55
  • So any ideas on best way to go about this? All i want is to display a list of the top 5 tags based on number of posts in that tag for each taxonomy I have. Commented Jun 25, 2013 at 16:57

1 Answer 1


I'm fairly certain that the taxonomies have nothing truly in common except posts that they share, so I think you can't avoid running an actual query of posts, in order to learn which categories are candidates for your criteria.

While, it may not be computationally the best method, you could do something like this...

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'AND',
            'taxonomy' => 'my_required_taxonomy',
            'field' => 'slug',
            'terms' => 'my_required_term',

$loop = new WP_Query($args);

$out_array = array();
while($loop->have_posts()) : $loop->the_post();
    $terms = get_the_terms($post->ID, 'my_variable_taxonomy');
    foreach($terms as $term){
        // note, can use term_id or whatever here instead
        if(!array_key_exists($out_array[$term->term_slug])) { 
            $out_array[$term->term_slug] = 1; 
        } else {

arsort($out_array); // edit: arsort is what you want

$slice = array_slice($out_array, 0, 5);

foreach($slice as $key => $value){
    $term = get_term_by('slug', $key, 'my_variable_taxonomy'); // post_tag
    $term_list .= '<a href="/term-base/' . $term->slug . '">'.$term->name.'</a>';

echo $term_list;
  • Hey ghosttoast, thanks. So I've put in my taxonomy name and the term, in my_variable_taxonomy i put post_tags but it's not generating anything? Am I missing something? Commented Jun 25, 2013 at 18:28
  • It seems so, or I have. Try doing a print_r($loop) to see what is returned from that. Also I realize my $term_list object contains no closing </a> tag or any real text - so this example would have no visible output. I'll modify answer to fix that.
    – GhostToast
    Commented Jun 25, 2013 at 19:21

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.