How to display related tags when using ExpressionEngine / Solspace Tag?


I’m a happy used of Solspace Tag for ExpressionEngine for We use it to bundle content for specific topics. However, I was looking for a way to display related tags. So for example if you’re looking at the privacy tag page you see that cookiewet is a strongly related topic.

Unfortunately this is not supported by default, but I see that many people were looking for this: “Newbie—I want to display related tags next to my related entries“, “How do I display related tags as a link from the Tag Cloud?“, “Tags Related to a Tag?“, “List of related tags“.

So I decided to use exp:query to write a query of my own to be able to pull this of.  Basically what it does is the following:

  1. It checks the current tag based on the segment, and outputs entry id’s separated by comma’s
  2. In an embed it feeds those entry id’s and the current entry id separately (so it can be excluded)
  3. In the embedded template, it checks which tags are connected to the supplied entry id’s, counts them and lists them in descending order based on the count. The output it limited to 5 tags.


{embed=”related_tag_query” entries=”{exp:tag:entries websafe_separator=”-” backspace=”1″ tag=”{segment_X}”}{entry_id},{/exp:tag:entries}” tag=”{exp:tag:tag_name websafe_separator=”-“}{segment_X}{/exp:tag:tag_name}”}


{exp:query sql=”SELECT exp_tag_tags.tag_name as tag_name, REPLACE(exp_tag_tags.tag_name,’ ‘, ‘-‘) as tag_name_websafe, count(exp_tag_entries.tag_id) as count_tag FROM exp_tag_entries LEFT JOIN exp_tag_tags ON exp_tag_tags.tag_id=exp_tag_entries.tag_id WHERE exp_tag_entries.entry_id IN({embed:entries}) AND exp_tag_entries.tag_group_id = 1 AND exp_tag_tags.tag_name != ‘{embed:tag}’ group by exp_tag_entries.tag_id order by count(exp_tag_entries.tag_id) desc limit 5″ backspace=”2″}<a href=”{site_url}tag/{tag_name_websafe}”>{tag_name}</a> ({count_tag}), {/exp:query}