Tags belong to the most fascinating things in WordPress. They organize content by semantics. Tags don’t care if a post is an essay, a quote, a gallery or a table. They tell you what the post is about.
Tags are all about meaning. And meaning depends on context. So shouldn’t tags as well?
The Same Thing, But Differently
Let me give you an example: Imagine I have an e-commerce website where I sell books. Technically, each book is a post, and as tags I use topics and author names. That way my customers can list all works of an author, and I can even use multiple authors per book. As categories I use types of publications such as “fiction”, “science”, “biography” and “reference”.
But then I discover that searching for authors can be ambiguous: I use the same tag “Edgar Allan Poe” both for Poe’s works and for books about Poe. Even though it is in both cases the same Poe, we are using his name in different meanings: Poe, the author, and Poe, the subject.
Is there a way how I could specify what I mean when I tag a book with “Poe”? Or: Is it possible to apply a tag only to a particular context? Clicking on “Edgar Allan Poe” usually takes me to a list of all books that are somehow related to him, but there should also be a way how to show only his works, or, alternatively, only books about him.
I quickly abandoned the idea to add the context in brackets, such as “Edgar Allan Poe (author)” and “Edgar Allan Poe (subject)”. I also experimented with a half-hearted workaround: I created two tags with the identical display name “Edgar Allan Poe” and different slugs,1 “edgar-allan-poe-author” and “edgar-allan-poe-subject”. But then it was impossible to know which oft them to use when editing a post because there you don’t see the slugs.
Adding Context on a Second Level
The solution that eventually proved to be the most practical and sustainable involves a second level to distinguish tags by their contexts. The tag “Edgar Allan Poe” appears now as a child under the parents “authors” and “subjects”. It is always the same tag, but each time in a different context.
The additional functionality was possible by developing the Tag Groups Premium plugin so that each tag can be a member of multiple groups. In each post I have now the option to select the tag in one of the available groups, or even all of them. “Edgar Allen Poe” can therefore appear both as an author and as the subject of book. Theoretically, a name can even appear as both – think of autobiographies.
How to Switch Between Global And Context-Aware Tags?
Let’s assume that the tags appear in a tag cloud that is organized in groups. That means that each group like “authors” or “subjects” has its own cloud of tags that appear in the respective context.
There are now two options how to handle these tags and which posts to show when clicking on a tag:
- With a global scope, each tag links to a list of all posts that use this tag. That is the default WordPress behavior.
- With a context-dependent scope, each tag links to a list of only those posts, that use this tag in that context where you clicked on it. So if you click on “Edgar Allan Poe” in the group “authors”, you will see all posts that are tagged with “Edgar Allan Poe” as an author, without any books about him.
You can imagine it as an additional filter: In the tag archives, posts are filtered by a particular tag. If you need to consider the context as well, you simply add one more filter.
This additional filter is applied through the query parameter term_group= … that is appended to the link in the tag cloud. If you omit this additional parameter, the tags are effective globally. If you add it, the tag group works as additional filter and you see only the corresponding subset of posts.2
Photo by Blowing Puffer Fish