Hoping someone can provide some insight on this one; I've been stuck on this last piece of the puzzle for a while and have yet to figure it out in Joomla 3.9. Apologies for the long explanation.

I need to filter com_content based on com_tags items in various places - category views, category articles, modules, featured views etc. A lot of this can be achieved with the Joomla core, but some areas and modules were found wanting, so I have patched those appropriately using a plugin onAfterRoute event, where I check for published tags, and insert those into the filter.tags state. This all works fine, except for subcategories.

The purpose of this is to have multiple menu items linked to categories which may or may not have subcategories. Each category may have one or more tags assigned to it, and should not be rendered if its tag is not published. This is working for filtering out articles in a category view, but does not filter out subcategories. I can see that the Category model does include a filter.tag and I am harnessing that by setting the appropriate tags in the filter.state and those categories without published tags are not being rendered.

However, where the category contains subcategories, with articles within those subcategories, I end up with a category view that displays all subcategories, navigating into one of those subcategories then displays the category view with the appropriately filtered articles.

As a simplified working example, the plugin can do this:

public function onAfterRoute() {
    $app = Factory::getApplication();
    $input = $app->input;
    $option = $input->getString('option', '');
    $view = $input->getString('view', '');
    $itemid = $input->getInt('id', 0) . ':' . $input->getInt('Itemid', 0);
    // Hardcoded tags. 
    $filterMyTags = array(1,3,5);

    if ($option === 'com_content' && $view === 'category') {
       $app->setUserState('com_content.category.filter.'.$itemid.'.tag', $filterMyTags);


The result of the above is that articles with tag ID's 1, 3 and 5 will rendered. Any content with other tags, will not be rendered.

However, this doesn't appear to work for subcategories rendered by blog_children.php, but the articles within the subcategory do get filtered, so the user ends up clicking into a subcategory where potentially all articles have been filtered, in which case I have had to extend the plugin to redirect the user back out, which is horrible. I'd rather filter the subcategory from the onset.

What am I missing? I located the category model in the administrator which does appear to have necessary filtering code (and it does; because you can filter categories by tag in the admin no problem). However, I think the recursion for getChildren() in the frontend com_content category model does not include any states except access and publication states.

Anybody fancy a stab in the dark at this one? Thanks!

  • It's using JCategories API. You'd probably have to override com_content's category helper class.
    – Sharky
    Feb 25 at 8:44
  • @Sharky late coming back to this, but yeah, that's pretty much what the solution was in the end - to override the JCategories model and associated classes, since just a simple filter wasn't enough - the query to get categories was fine, but then if you join on the tag table, those categories you expect to be filtered would throw a 404. Without going too far down the rabbit hole, I'd prefer to be able to capture those instead of throwing the 404, thus, we can easily navigate the user to a meaningful place in the UI, which is exactly what overriding JCategories has allowed.
    – Grant G
    Mar 1 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.