Skip to content

Fix: Queried category should appear first in post category list when filtering by category#8373

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-random-category-display
Draft

Fix: Queried category should appear first in post category list when filtering by category#8373
Copilot wants to merge 2 commits intomainfrom
copilot/fix-random-category-display

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 27, 2026

When a post belongs to multiple categories and a visitor filters posts by a specific category, the post card displays a random category name instead of the one being filtered — because the backend returns all post categories in stored order and templates display the first one.

Changes

  • PostFinderImpl.listByCategory(): After fetching posts, reorder each post's categories list so the queried category appears first, followed by matched child categories, then remaining categories.
  • PostFinderImpl.list(Map params): Same fix applied when categoryName filter is present.
  • sortCategoriesByFilter() helper: Extracted shared sorting logic; uses HashSet for O(n log n) sort instead of O(n²·m).
// Before: post filtered by "cat1" could return categories as ["cat2", "cat1"]
// After: categories are ["cat1", "cat2"] — queried category is always first
private static void sortCategoriesByFilter(ListedPostVo postVo, String queriedCategoryName,
    List<String> matchedCategoryNames) {
    var matchedSet = new HashSet<>(matchedCategoryNames);
    reordered.sort((a, b) -> {
        boolean aExact = queriedCategoryName.equals(nameA);
        boolean bExact = queriedCategoryName.equals(nameB);
        // exact match first, then child matches, then rest
        ...
    });
}
  • Tests: Added ListByCategoryTest covering multi-category reordering and single-category no-op cases.

What type of PR is this?

/kind bug
/kind regression

What this PR does / why we need it:

When filtering posts by category on the visitor-facing site, each post's categories list was returned in stored order. Since templates display the first category, a post with ["cat2", "cat1"] filtered by cat1 would show cat2 on the card. The fix reorders categories per-post so the filtered category is always first.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

The reordering is applied only in the two category-filtered list paths (listByCategory and list(Map) with categoryName). Other listing paths (by tag, owner, archives) are unaffected.

Does this PR introduce a user-facing change?

Fix post card displaying wrong category name when filtering posts by a category that the post shares with other categories.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkstyle.org
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/xml/tools/xml-extractor.jar --fileList=/tmp/codeql-scratch-dd1d0a2b113c8104/dbs/java/working/files-to-index3278415875141933549.list --sourceArchiveDir=/tmp/codeql-scratch-dd1d0a2b113c8104/dbs/java/src --outputDir=/tmp/codeql-scratch-dd1d0a2b113c8104/dbs/java/trap/java (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>当给文章选择多个分类时,文章发布后,文章的分类名称随机显示</issue_title>
<issue_description>### 前置条件

  • 已经在 issues 列表中搜索了相关问题。
  • 这是 Halo 项目本身存在的问题,如果是非项目本身的问题(如:安装部署问题),建议在 Discussions 提交。
  • 已经尝试过停用所有的插件,排除是插件导致的问题。
  • 如果是插件和主题的问题,请在对应的插件和主题仓库提交。

系统信息

使用的哪种方式运行?

Docker

发生了什么?

当给文章选择多个分类时,文章发布后,文章的分类名称随机显示

复现步骤

1、新建文章,给它添加2个分类,文章分类1、文章分类2
2、文章发布,进入访客主页,查看刚才发布的文章
3、通过访客主页的文章分类菜单筛选不同的文章(卡片)
4、刚才那篇文章卡片顶端显示的分类名称随机显示,一会显示【文章分类1】、一会儿显示【文章分类2】。我在筛选文章分类1 的时候,它反而显示成文章分类2,筛选文章分类2的时候,反而显示成文章分类1。这个是随机的,有时候显示正常。

期望结果:
当文章同时属于2个分类时,在筛选文章列表时,文章卡片顶端显示的分类名称应该是当前分类,而不是随机显示。
注意:使用的主题是2.0默认的earth。

相关日志输出

附加信息

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@f2c-ci-robot f2c-ci-robot bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 27, 2026
@f2c-ci-robot f2c-ci-robot bot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Feb 27, 2026
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

…gory

Co-authored-by: ruibaby <21301288+ruibaby@users.noreply.github.com>
@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Feb 27, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from ruibaby. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sonarqubecloud
Copy link
Copy Markdown

Copilot AI changed the title [WIP] Fix random display of article category names on publish Fix: Queried category should appear first in post category list when filtering by category Feb 27, 2026
@f2c-ci-robot f2c-ci-robot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note Denotes a PR that will be considered when it comes time to generate release notes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

当给文章选择多个分类时,文章发布后,文章的分类名称随机显示

3 participants