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.

Is it possible to just show the most recent tags from e.g. the latest 10 posts as a tag cloud?

The only things that I found were the WP Recent Tags plugin which is a bit outdated and no tags are displayed when I use the widget.

I found the following posts:

How can I get a list of the most recent tags in a wordpress database?
Get recent tags in wordpress

But they don’t work for me either.

share|improve this question

migrated from stackoverflow.com Sep 30 '12 at 22:36

This question came from our site for professional and enthusiast programmers.

add comment

1 Answer 1

up vote 2 down vote accepted

Sure, just feed your tag list to wp_generate_tag_cloud:

$found_tags = $final_tags = $id_records = array();

// get last 10 posts
$last_posts = get_posts('numberposts=10');

// gather tags
foreach($last_posts as $post)
  $found_tags = array_merge($found_tags, wp_get_post_tags($post->ID));

// prepare final tags for the cloud
foreach($found_tags as $tag){

  // ignore duplicates
  if(in_array($tag->term_id, $id_records))
    continue;

  // track ids...
  $id_records[] = $tag->term_id;

  // generate links
  $tag->link = get_tag_link($tag);

  // keep it
  $final_tags[] = $tag;
}

// feed to the cloud
print wp_generate_tag_cloud($final_tags);

You might want to cache this, because it makes quite a few queries

share|improve this answer
    
Thank you very much—works like a charm! Just another short question: Is it possible to sort the tags by count and not by name? –  user1706680 Oct 17 '12 at 16:18
    
I just found out how to sort the tags by count: Changing the last line to print wp_generate_tag_cloud($final_tags, array('orderby' => 'count', 'order' => 'DESC')); will do it. –  user1706680 Oct 19 '12 at 7:44
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.