Working with Tags in Joomla

Tags are stored in the tags table using a tree hierarchy structure. You can use TagsHelper class which provide access to the tag data.

TagsHelper Functions

There are also three TagsHelper functions which provide access to tag data:

1. getTagNames()

You provide an array of tag ids and get returned an array of the corresponding tag titles.

$th = new TagsHelper();
$tagnames = $th->getTagNames(array('2', '3')); // returns something like array('tag2', 'tag3')

2. getTagTreeArray()

You provide the id of the tag and get returned an array containing the id you passed in, along with the ids of the children in the tree hierarchy. Tag ids are returned as strings.

$th = new TagsHelper();
$result = $th->getTagTreeArray($tagid);

3. searchTags()

You provide an associative array of filters and you get back an array of the tags which match the filters.  The filters include language (specified as "flanguage"), title/path (specified as "like"), published state (specified as "published"), access (specified as "access").

$filters = array('like' => "tag");
$results = TagsHelper::searchTags($filters); // this is a static function
foreach ($results as $result)
echo "Id: " . $result->value . ", Title Path: " . $result->text . ", Path: " . $result->path;

Tags: Content Database Structure

The relationship between tag and the records of Joomla items such as articles and contacts is a many-many relationship. So, this is resolved through the use of a join table contentitem_tag_map, which contains:

  1. tag_id
  2. type_alias 
  3. content_item_id

The tag_id points to the tag record.

The type_alias is of the form "com_content.article", "com_content.category", "", and so on. This defines the type of item, and hence the database table to find that item in.

The content_item_id is the id of the record in that item table.

How to Get Tags Associated with Item

You can get the tags associated with an individual item through the following two functions:

1. getTagIds()

You pass in the component and id or ids of the component items. The function returns a string of the ids of the tags associated with the items. This function is often used to obtain the current tag ids associated with an item for editing purposes.

$th = new TagsHelper();
$result = $th->getTagIds(23, "com_content.article");

2. getItemTags()

This is similar to getTagIds() in that it gets the tags associated with the item you pass in, but it also allows you to get all the values of the fields in the tags table for those records. This function is often used on the site front-end when displaying the tags associated with an item.

$th = new TagsHelper();
$result = $th->getItemTags("com_content.article", 23);

How to Get Items Associated with Tag

1. getTagItemsQuery()

You can find the items which are associated with a given tag (or array of tags) using getTagItemsQuery(). This returns a query that you can run to get details of the items which have the passed-in tag id or ids.

$th = new TagsHelper();
$query = $th->getTagItemsQuery($tagid); // query for content which has the tag with id = $tagid
$db = Factory::getDbo();
$results = $db->loadAssocList();
foreach ($results as $result)
echo "<br>Type alias: " . $result["type_alias"] . ",Id: " . $result["content_item_id"] . ",Title: " . $result["core_title"];

Associating Tags with Items

To associate a tag with an item, use Joomla Table functionality for the item's table. Set the Table instance property newTags to be an array of the ids of the tags you want that item to have. For example:

$table = Table::getInstance('Content');
$table->load("2"); // load article which has id=2
$table->newTags = array("1", "4");

If your model inherits from the AdminModel, then when you do


The AdminModel code sets the newTags property for you in the line

$table->newTags = $data['tags'];

When Updating Items

If you are updating an item with tags using the Table functionality, and you don't specify the newTags property, then all the existing tag associations for that item will be removed. To maintain them:

$th = new TagsHelper();
$currentTags = $th->getTagIds("1", "com_content.article"); // insert your item here
$table->newTags = explode(',',$currentTags);
// before you call
$table->store(); // or $table->save();

If you want to remove all tags from an item, then you can omit the newTags property completely, or set it to null, and then call $table->store().