# 标签“无法在此类别中使用” - 当标签受限时

**URL:** https://meta.discourse.org/t/tag-cant-be-used-in-this-category-when-restricted-tag/407050
**Category:** Bug
**Tags:** tags, fixed
**Created:** [2026年七月8日 05:11 UTC](https://meta.discourse.org/t/tag-cant-be-used-in-this-category-when-restricted-tag/407050 "2026-07-08T05:11:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [2026年七月8日 05:11 UTC](https://meta.discourse.org/t/tag-cant-be-used-in-this-category-when-restricted-tag/407050/1 "2026-07-08T05:11:23Z")

</div>

我有一个仅限管理员使用的标签，但当我尝试使用它（作为管理员）时，它一直提示“无法在此类别中使用”。之前一切正常，但最新的更新似乎导致了问题。

---

<div class="post-metadata">

### Author: ![nat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nat/32/235063_2.png) [@nat](https://meta.discourse.org/u/nat)
#### Post date: [2026年七月9日 14:03 UTC](https://meta.discourse.org/t/tag-cant-be-used-in-this-category-when-restricted-tag/407050/2 "2026-07-09T14:03:59Z")

</div>

你能检查一下你的分类设置，看看在你创建主题的类别中这些是否已填写？

 ![hmm](https://global.discourse-cdn.com/meta/original/4X/9/8/6/986fa5b0d5d2ac8517c029a172168b3c2d6511ef.png)

我怀疑最近可能有一些“安全”类型的更改，这可能导致设置比以前更严格。

---

<div class="post-metadata">

### Author: ![AstonJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/astonj/32/215041_2.png) [@AstonJ](https://meta.discourse.org/u/AstonJ)
#### Post date: [2026年七月11日 02:20 UTC](https://meta.discourse.org/t/tag-cant-be-used-in-this-category-when-restricted-tag/407050/3 "2026-07-11T02:20:49Z")

</div>

我尝试了多个类别（大多数类别没有标签限制）。该标签也没有被添加到任何类别限制中。

我甚至创建了一个新标签，并通过“标签组”将其设置为“ **仅以下组可见** ”，并添加了 `admins` 类别——所以我应该能够使用它……但却不行。

奇怪的是，一开始我可以使用它，但过了一天再回去看，就不能用了。

当未选择类别时，显示 `无法使用`；当选择某个类别时，则显示 `在此类别中无法使用`。

---

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [2026年七月11日 05:39 UTC](https://meta.discourse.org/t/tag-cant-be-used-in-this-category-when-restricted-tag/407050/4 "2026-07-11T05:39:15Z")

</div>

我编写了这段数据探索查询，以找出为什么我一年前无法使用该标签的限制原因。它检查标签或标签组是否仅限于其他类别，或者类别是否仅限于其他标签。这算是 [Group tag in new topic doesn't show the tags - #2 by Moin](https://meta.discourse.org/t/group-tag-in-new-topic-doesnt-show-the-tags/368348/2) 的论坛范围概览。

```sql
-- [params]
-- null string :tag_name
-- null string :tag_group

WITH allowed_tags AS (
  SELECT ct.category_id, t.id AS tag_id
  FROM category_tags ct
  JOIN tags t ON t.id = ct.tag_id
  UNION
  SELECT ctg.category_id, tgm.tag_id
  FROM category_tag_groups ctg
  JOIN tag_group_memberships tgm ON tgm.tag_group_id = ctg.tag_group_id
),

restricted_categories AS (
  SELECT c.id, c.name
  FROM categories c
  WHERE c.allow_global_tags = FALSE
    AND (
      EXISTS (SELECT 1 FROM category_tags ct WHERE ct.category_id = c.id)
      OR EXISTS (SELECT 1 FROM category_tag_groups ctg WHERE ctg.category_id = c.id)
    )
),

all_tag_category_combinations AS (
  SELECT t.id AS tag_id, t.name AS tag_name, rc.id AS category_id, rc.name AS category_name
  FROM tags t
  CROSS JOIN restricted_categories rc
  WHERE t.target_tag_id IS NULL
),

not_allowed_tags AS (
  SELECT atcc.tag_id, atcc.category_id, atcc.category_name
  FROM all_tag_category_combinations atcc
  LEFT JOIN allowed_tags al
    ON al.tag_id = atcc.tag_id AND al.category_id = atcc.category_id
  WHERE al.tag_id IS NULL
)

SELECT
  main.id AS tag_id,
  main.name AS tag_name,
  COALESCE(STRING_AGG(DISTINCT syn.name, ', '), '') AS synonyms,

  tg.id AS tag_group_id,
  COALESCE(parent_tag.name, '') AS parent_tag_name,  
  CASE WHEN tg.one_per_topic THEN 'true' ELSE '' END AS one_tag_per_topic,

  COALESCE(
    STRING_AGG(
      DISTINCT
      CASE
        WHEN crtg.min_count IS NOT NULL THEN crtg_cat.name || ' (' || crtg.min_count || ')'
        ELSE NULL
      END,
      ', '
    ),
    ''
  ) AS required_in_categories_with_min,

  COALESCE(STRING_AGG(DISTINCT ctg_cat.name, ', '), '') AS tag_group_limited_to_category,
  COALESCE(STRING_AGG(DISTINCT ctags_cat.name, ', '), '') AS tag_limited_to_category,
  COALESCE(STRING_AGG(DISTINCT nat.category_name, ', '), '') AS not_allowed_in_categories,

  CASE 
    WHEN COUNT(DISTINCT g.name) > 0 THEN COALESCE(STRING_AGG(DISTINCT g_use.name, ', '), '')
    ELSE ''
  END AS use_limited_to,
  COALESCE(STRING_AGG(DISTINCT g.name, ', '), '') AS view_only_groups

FROM tags AS main
LEFT JOIN tags AS syn ON syn.target_tag_id = main.id
LEFT JOIN tag_group_memberships AS tgm ON tgm.tag_id = main.id
LEFT JOIN tag_groups AS tg ON tg.id = tgm.tag_group_id
LEFT JOIN tags AS parent_tag ON parent_tag.id = tg.parent_tag_id
LEFT JOIN tag_group_permissions tgp ON tgp.tag_group_id = tg.id AND tgp.permission_type = 3
LEFT JOIN groups g ON g.id = tgp.group_id

LEFT JOIN category_required_tag_groups AS crtg ON crtg.tag_group_id = tg.id
LEFT JOIN categories AS crtg_cat ON crtg.category_id = crtg_cat.id
LEFT JOIN category_tag_groups AS ctg ON ctg.tag_group_id = tg.id
LEFT JOIN categories AS ctg_cat ON ctg.category_id = ctg_cat.id
LEFT JOIN category_tags AS ctags ON ctags.tag_id = main.id
LEFT JOIN categories AS ctags_cat ON ctags.category_id = ctags_cat.id
LEFT JOIN not_allowed_tags AS nat ON nat.tag_id = main.id
LEFT JOIN tag_group_permissions tgp_use ON tgp_use.tag_group_id = tg.id AND tgp_use.permission_type = 1
LEFT JOIN groups g_use ON g_use.id = tgp_use.group_id

WHERE
  main.target_tag_id IS NULL AND
  (
    :tag_name IS NULL OR
    LOWER(main.name) LIKE LOWER('%' || :tag_name || '%') OR
    LOWER(syn.name) LIKE LOWER('%' || :tag_name || '%')
  ) AND
  (
    :tag_group IS NULL OR
    LOWER(tg.name) LIKE LOWER('%' || :tag_group || '%')
  )

GROUP BY
  main.id, main.name, tg.id, tg.name, parent_tag.name, tg.one_per_topic
ORDER BY main.name, tag_group_id

```

由于管理员通常即使在使用受限时也能使用标签，我不确定这是否对你的情况有帮助。但我觉得值得一试。

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2026年七月22日 14:26 UTC](https://meta.discourse.org/t/tag-cant-be-used-in-this-category-when-restricted-tag/407050/6 "2026-07-22T14:26:17Z")

</div>

感谢 @AstonJ 的报告 👍 这将修复该问题，并有望通过统一应用中可见/可使用标签的范围来解决其他相关问题。

> <https://github.com/discourse/discourse/pull/41918>
>
> Previously, a tag whose only usage was in personal messages was silently dropped… by \`TagsController.tag\_counts\_json\` — a display rule from 2020 meant to keep such tags off the \`/tags\` browse page for users who cannot tag messages (and \`pm\_tags\_allowed\_for\_groups\` has no staff bypass, so by default that includes admins). Every surface reusing that method as a plain serializer inherited the rule by accident:
> 
> \- the composer tag search treated the missing row as unauthorized and showed the tag disabled with a bogus \*\*"Can't be used in this category"\*\* reason (the reported bug),
> \- every "show all tags" chooser (tag groups, synonyms, watched tags, category allowed tags, webhooks, automations, …) silently refused to offer such tags at all,
> \- the \`#\` autocomplete would not suggest a tag that nonetheless cooked into a working hashtag link when typed in full.
> 
> This change makes \`tag\_counts\_json\` a pure serializer and moves the rule into an explicit, named helper (\`DiscourseTagging.without\_pm\_only\_tags\`) applied only where it belongs — the \`/tags\` browse lists — with an exemption for the admin "show all tags" view so the admin inventory is complete. Selection and search surfaces now offer every tag the user is allowed to use, and tag-group visibility rules still apply everywhere.
> 
> It also fixes two adjacent inconsistencies uncovered along the way:
> 
> \- \*\*Topic→message conversion counter drift.\*\* Converting only adjusted \`public\_topic\_count\`, so a converted topic's tags kept working until the periodic consistency job recounted them into the broken state — the "worked at first, broke a day later" in the report. The converter now moves all three counters immediately, and rolls back cleanly when the underlying post revision fails (its return value was previously ignored, and \`Topic#valid?\` clears the errors it adds, so a failed conversion still applied its side effects).
> \- \*\*Crawler/print tag leak.\*\* The crawler layout leaked a message's tag names in the page title and \`og:article:tag\` metadata to participants the serializer already hides tags from; both now flow through \`TopicView#visible\_tags\`, gated on \`guardian.can\_see\_tags?\`.
> 
> Reported in https://meta.discourse.org/t/407050

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2026年七月28日 16:54 UTC](https://meta.discourse.org/t/tag-cant-be-used-in-this-category-when-restricted-tag/407050/8 "2026-07-28T16:54:50Z")

</div>


