1

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:

https://stackoverflow.com/questions/2972525/how-can-i-get-a-list-of-the-most-recent-tags-in-a-wordpress-database
https://stackoverflow.com/questions/10104947/get-recent-tags-in-wordpress

But they don’t work for me either.

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

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

4

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

  • 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
  • That first line tho... I didn't know you could do that but it makes perfect sense for defining variables. – Slam Apr 18 '18 at 6:03

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.