Skip to content

🐛 Fixed "primary tag is not" filters leaving out posts with no primary tag - #323

Open
rob-ghost wants to merge 1 commit into
mainfrom
fix/dya-361-negated-primary-tag
Open

rob-ghost wants to merge 1 commit into
mainfrom
fix/dya-361-negated-primary-tag

Conversation

@rob-ghost

@rob-ghost rob-ghost commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

ref https://linear.app/ghost/issue/DYA-361

Problem

primary_tag stands for the slug of a post's first tag, when that tag is public. Ghost's posts expand it to tags.slug:x+posts_tags.sort_order:0+tags.visibility:public, and primary_author expands the same way. A negation expanded in place negated the slug alone and kept the other conditions, so primary_tag:-blog meant "has a public primary tag that isn't blog". It left out every post with no primary tag, and every post whose first tag is internal.

Ghost's routes.yaml matching reads primary_tag as one value and keeps those posts. On a site whose home page collection filters on primary_tag:-blog (forum answers recommend it), an untagged post gets a URL in that collection but never appears on its index or pages (Ghost#11728).

Several negations beside another condition were worse. primary_tag:-a+primary_tag:-b+featured:true dropped featured:true. combineNeFilters wrote the combined $nin beside the $and group instead of in it, and expanding the key then replaced that group.

Solution

  • Negations of an expanded key with conditions. They are expanded as the negated group of what they deny, so primary_tag:-blog selects what the existing workaround -(primary_tag:blog) selects:

    nql('primary_tag:-blog', ghostPostOptions).parse()
    // {$nor: [{$and: [{'tags.slug': 'blog'}, {'posts_tags.sort_order': 0}, {'tags.visibility': 'public'}]}]}

    This covers one value (-v), several (-[a,b], and -a+-b once combined) and "does not contain" (-~), on every expansion that adds conditions. A key that only renames another (tag, author) expands in place as before. So does a negation that names null, such as primary_tag:-null+primary_tag:-x: it also says the key has a value, and it already selects the right posts.

  • Where the combined $nin goes. It now takes the place of the first negation inside the $and, for every key, rather than only being skipped for expanded keys. The fault was where it was written, which loses a condition whenever anything later rewrites the group. Moving it is as small as skipping expanded keys, and keeps the combined subquery. For an expanded key, the negated group already keeps the condition beside one denied value or list. The move is what keeps it when the list names null, as in primary_tag:-null+primary_tag:-tech+featured:false. For a key that only renames another, the move changes rows only where a join table condition sits beside the negations. Main dropped that condition, and it now applies.

What changes, for which filters

Filter Before After Exposure
primary_tag:-blog, primary_tag:-[a,b], primary_tag:-~'x' only posts with a public primary tag other than blog every post whose primary tag isn't blog, including posts with none or with an internal first tag Hand-written public: routes.yaml, themes, Content API. Forum answers and small public sites use filter: primary_tag:-blog for the home collection. Index pages list more posts: the ones that already had URLs there
primary_tag:-a+primary_tag:-b with another condition beside it, also primary_author the condition beside them dropped kept Hand-written public. A public routes.yaml uses primary_tag:-a+primary_tag:-b+primary_tag:-c
primary_author:-x, primary_author:-[a,b] posts with a public primary author other than x every post whose primary author isn't x Hand-written public. The same posts in practice: every Ghost post has an author, and staff users are public
primary_tag:-null+primary_tag:-tech+featured:false featured:false dropped: posts 1 and 6 in the tests kept: post 6 only Hand-written public
-(primary_tag:-news) posts whose primary tag is news, and posts with none posts whose primary tag is news: fewer rows Hand-written public
primary_tag:-news+tag:news a news tag but a public primary tag other than news a news tag and a primary tag other than news, or none Hand-written public
tag:-a+tag:-b with a join table condition beside them: tag:-news+tag:-tech+posts_tags.sort_order:0 the join table condition dropped: posts with neither tag at all posts whose first tag is neither, as written Hand-written private and public. Only hand-written join table keys reach it
k:-a+k:-b with any other condition beside them, any other key same rows. toJSON() and the SQL nest the combined list inside the group Ghost's posts SQL text; nothing Ghost pins
primary_tag:-null alone, primary_tag:null, positive primary_tag:x unchanged

In memory

The new tests match the same filters against posts shaped as Ghost's URL service sees them: the primary tag is the first tag if public, and the primary author is the first author. They use Ghost's routes.yaml expansions, which map primary_tag to primary_tag.slug, and select the same posts as the database for every filter tested. Three places can't be compared:

  • primary_tag:-null alone. The database still selects no post, because null on a many-to-many relation is read the other way round. Not changed here.
  • Join table keys. They have no form on a post object.
  • primary_author and visibility. In memory it is the first author whatever that author's visibility, and in the database a public one.

Left as it is

  • primary_tag:-null alone and primary_tag:null still select no post in the database. Null on a many-to-many relation is read the other way round, and fixing that was dropped. In memory they select the posts with a primary tag and without one.
  • Ghost's own expansions keep the in-place negation. Ghost's members keyWithConditionStep and the activity feed's transformers in event-repository.js call mongo-utils' expandFilters directly, not nql's. That is harmless today:
    • Members: the only key there with a condition is opened_emails.post_id, and its condition (email_recipients.opened_at:>=0) is on the join table of the same relation. Its negation already compiles to one NOT IN with that condition inside, which is the complement.
    • Activity feed: its data.post_id (with attribution_type:post) is never written negated by Ghost or Admin.

Ghost

  • Posts compile through nql's own expansions, so the catalog bump is enough, with no code change. Nothing Ghost builds negates primary_tag or primary_author, and no Ghost test pins these queries.
  • Members expand before they combine, so they never lost a condition this way. No Ghost pull request is needed for members.

Release

Each of the three filter fixes needs a minor version, since rows change for existing filters:

Package Version Pull request
@tryghost/mongo-knex 0.14.0 #321
@tryghost/nql-lang 0.9.0 #322
@tryghost/nql 0.15.0 #323

Ship them in one run, after all three merge, so nql's ranges move to the new mongo-knex and nql-lang:

pnpm ship:minor --projects=@tryghost/mongo-knex,@tryghost/nql-lang,@tryghost/nql

Leave out --projects and the run also takes @tryghost/mongo-utils to 0.8.0, which has no changes, and moves nql's range to ^0.8.0. Ghost pins mongo-utils 0.7.0 exactly, so it would then load two copies.

A dry run of the command above (pnpm exec nx release version minor --projects=… --dry-run) bumps exactly the three packages and moves both of nql's ranges.

Ghost then bumps mongo-knex, nql and nql-lang in one pull request. Its members pipeline calls mongo-knex directly, while posts and comments compile through nql's own mongo-knex. Bumping one without the other runs two copies that disagree. That pull request also updates the two SQL strings that ghost/test/unit/server/models/post.test.js pins: posts.id != ? becomes (posts.id is null or posts.id != ?).

Testing

New integration tests on posts with tags and authors, using Ghost's real post relations and expansions. They check rows from the database and in memory for every filter above, including under an enforced status:published as the Content API applies one. Unit tests cover the expanded JSON and the combined $nin's place. The negation tests fail before this change, and putting the combined $nin back beside the group fails primary_tag:-null+primary_tag:-tech+featured:false.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0aee75ab-4756-4dc9-9665-2ba6b8e4f2b7
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@codecov-commenter

codecov-commenter commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.81%. Comparing base (e0689bd) to head (bb4ae71).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #323      +/-   ##
==========================================
+ Coverage   85.56%   85.81%   +0.24%     
==========================================
  Files           9        9              
  Lines        2418     2460      +42     
  Branches      510      522      +12     
==========================================
+ Hits         2069     2111      +42     
  Misses        343      343              
  Partials        6        6              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…y tag

ref https://linear.app/ghost/issue/DYA-361

`primary_tag` stands for the slug of a post's first tag when that tag is public, so `primary_tag:-blog` should select every post whose primary tag isn't blog, including posts with none. The expansion negated the slug alone and kept its other conditions, so the filter only selected posts that have a public first tag. Ghost's routes.yaml matching reads the key as one value and keeps the posts without one, so on a site whose home page collection filters on `primary_tag:-blog`, an untagged post got a URL in that collection but never appeared on its index. A negation of a key whose expansion adds conditions now expands as the negated group of what it denies, as `-(primary_tag:blog)` already did, for one value, several values and "does not contain", on `primary_tag`, `primary_author` and any such key. Several negations of one key beside another condition, as in `primary_tag:-a+primary_tag:-b+featured:true`, also lost that condition: combining them into one list wrote the list beside the group of conditions instead of in it, and expanding the key then replaced the group. The combined list now takes the place of the first negation in the group, for every key. For a key that only renames another, that changes rows only where a join table condition sat beside the negations: `tag:-news+tag:-tech+posts_tags.sort_order:0` lost that condition and selected the posts with neither tag at all, and now selects the posts whose first tag is neither. A negation that also says the key has a value, such as `primary_tag:-null`, expands as before.
@rob-ghost
rob-ghost force-pushed the fix/dya-361-negated-primary-tag branch from f284356 to bb4ae71 Compare November 30, 2026 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants