Accurate topic count in tag list

In the tags page, the topic number is not accurate. Indeed, it includes topics that are not visible to the user.

Example here on meta:

I’ve been hit by this issue when using the API. In my plugin, I need an exact topic count, so I’m forced to load each tag in order to count the visible topics. It seems to work right now, but it’s a waste of bandwidth and I’m afraid one day I will hit a Discourse rate limit for API calls.

Any chance this might be solved?



Those are all in PMs you don’t have access to.

It’s not a fix or anything (obviously) but at 4 instances only in PMs I don’t think we need this particular tag … so I’m removing it.

1 Like

If need be, another example on meta is the sass tag (topic count in tag list=2, topic count in tag page=1).

1 Like

I’ve noticed this in our forum also. The easiest way to recreate it is if i create a new topic and tag then delete it, it is still counted.


An additional minor annoyance caused by this issue is that admins get a wrong error message when deleting an empty tag.

Empty tag:

Wrong error message:

Can you repro this @tshenry?

1 Like

Ok, I’m re-doing my response as I’m pretty sure I figured out everything that’s going on here :slight_smile:

As @ninjapenguin mentions, if you have a tag and delete all of the topics that have that tag applied, the tag remains listed on the tags page and the count unchanged. I believe this is happening because the topics still technically exist and they continue to hold the tag.

@syl if you navigate to the tete tag’s topic list as shown in your screenshot and then add


to the end of the URL, you should see two deleted topics listed with the tete tag.

The information message in your second screenshot corresponds to the the number of deleted topics that have the tag applied.

I feel like we probably shouldn’t factor in deleted topics when counting/listing tags, but this may not be a super trivial change given how everything is stored in the database. What do you think @codinghorror?


That is correct.

In a standard forum, I think deleted topics explain most discrepancies between the topic count displayed in the tag list and the actual number of topics a user can see within each tag. Fixing this would already be a big plus. But notice that discrepancies also occur when a non-admin user is looking at a tag containing private topics.

This I cannot reproduce. If I have a tag that has only been used on a PM or a topic in a restricted category, a user without access to those topics does not see the tag included in the tag list.

The only unfortunate behavior I’m seeing at the moment is what I mentioned surrounding deleted topics.

1 Like

Sorry for the confusion, I was talking about the tag count. Here is how to reproduce the issue:

  1. As an admin, create a topic with no category and with a new tag
  2. As an admin, create another topic with the same tag and belonging to a restricted category
  3. As a normal user, go to the test-tag tag and check that you can see only one topic
  4. But in the tag list, the tag count shows 2
1 Like

Ok thanks, I see that now.

I took a look at the code and have noticed a few things.

  • There are two columns in the tags table that can influence the tag count: topic_count and pm_topic_count. These counts are incremented/decremented on tag application/removal. Non-admin users only ever see the value of topic_count.

  • There is a consistency job that runs every 12 hours that will update the tag counts so that they do not include deleted topics.

  • There is nothing to adjust the count on the fly based on a user’s individual topic access.

Based on what I’m seeing, we don’t have a bug, just a limitation based on the current implementation. I think everything that has been talked about here falls under the “feature request” banner and can be summarized as:

Make tag counts on the tags page dynamic so that they reflect what a user will actually see when they navigate to the tag’s topic list.

1 Like

Thanks a lot @tshenry. That sounds very good, and might explain why a got the issue a lot more on my dev instance (maybe it doesn’t run the consistency job). I will check this.

P.S.: right now, the documentation tag here on Meta has a count of 5 but only 4 topics that I can see. I will check again in 12 hours :slight_smile:

1 Like