Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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( 
    'post_tag'
);

$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) {
        $i++;
        $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?

share|improve this question
    
"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 Jun 25 '13 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. –  Seth Rubenstein Jun 25 '13 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. –  Seth Rubenstein Jun 25 '13 at 16:57
add comment

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',
        array(
            '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 {
            $out_array[$term->term_slug]++;
        }
    }
endwhile;
wp_reset_query();

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;
share|improve this answer
    
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? –  Seth Rubenstein Jun 25 '13 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 Jun 25 '13 at 19:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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