Skip to content

Add a setting to exclude the current post from a query loop, without NOT IN - #40

Closed
tomjn wants to merge 3 commits into
mainfrom
exclude-current-post
Closed

tomjn wants to merge 3 commits into
mainfrom
exclude-current-post

Conversation

@tomjn

@tomjn tomjn commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Suggested posts on a single post template should leave out the post being read, and the Query Loop block has no way to do that. Sites end up writing their own query_loop_block_query_vars filter with post__not_in, which VIP's code scanner flags because NOT IN queries scale poorly, creating temporary tables and memory load on the database server.

This adds an "Exclude current post" toggle to the Extra Query Loop Settings, shown on loops that don't inherit the query. When viewing post A, post A is left out and the loop still shows its full number of posts.

The loop's first page doesn't use NOT IN. It fetches one extra post and drops the current post, or the spare one, in PHP before the results are tracked as displayed. Page 2 onwards and the queries pagination blocks run to count pages still use post__not_in, because fetching an extra post there would shift offsets and page counts. So only paginated loops pay for NOT IN, and a typical suggested posts loop never does. Loops that use post__in drop the ID from that list instead.

The setting does nothing in editor previews, since there's no current post there. "Exclude already displayed posts" could use the same extra fetch later, but it would need to fetch as many extra posts as are already shown, so I've left it out of this PR.

Why not extend "Exclude already displayed posts"? That setting tracks posts shown by earlier query loops, and the current post on a single template isn't shown by a loop. Counting it as displayed would change what existing loops show on single posts after an update, and it would tie two separate choices together: some loops want no repeats from earlier loops but can show the current post, and others the reverse.

Suggested posts on a single post template need to leave out the post being read. post__not_in scales poorly, so the loop's first page fetches one extra post and drops the current one in PHP. Later pages and pagination counts still use post__not_in so offsets stay exact.
@tomjn
tomjn requested a review from roborourke September 21, 2026 12:48
@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Playwright test results — WP 6.9

passed  27 passed

Details

stats  27 tests across 9 suites
duration  1 minute, 26 seconds
commit  1e430a0

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Playwright test results — WP 7.0

failed  8 failed
passed  19 passed

Details

stats  27 tests across 9 suites
duration  6 minutes, 3 seconds
commit  1e430a0

Failed tests

chromium › elasticpress-toggle.spec.js › ElasticPress Toggle › should show the Use ElasticSearch toggle in the panel
chromium › elasticpress-toggle.spec.js › ElasticPress Toggle › should toggle the Use ElasticSearch setting on and off
chromium › posts-per-page.spec.js › Posts Per Page Override › should show posts per page control when query inherits
chromium › posts-per-page.spec.js › Posts Per Page Override › should reflect posts per page override in editor
chromium › posts-per-page.spec.js › Posts Per Page Override › should enforce max value from posts_per_page setting
chromium › query-presets.spec.js › Query Presets › should show query preset dropdown when presets are registered
chromium › query-presets.spec.js › Query Presets › should change post order when selecting alphabetical preset in editor
chromium › query-presets.spec.js › Query Presets › should change post order when selecting Z-A preset in editor

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Playwright test results — WP 7.1

failed  11 failed
passed  16 passed

Details

stats  27 tests across 9 suites
duration  8 minutes, 32 seconds
commit  1e430a0

Failed tests

chromium › elasticpress-toggle.spec.js › ElasticPress Toggle › should show the Use ElasticSearch toggle in the panel
chromium › elasticpress-toggle.spec.js › ElasticPress Toggle › should toggle the Use ElasticSearch setting on and off
chromium › multiple-post-templates.spec.js › Multiple Post Templates › should support multiple post-template blocks with different post limits
chromium › multiple-post-templates.spec.js › Multiple Post Templates › should automatically limit third post template to remaining posts
chromium › posts-per-page.spec.js › Posts Per Page Override › should show posts per page control when query inherits
chromium › posts-per-page.spec.js › Posts Per Page Override › should reflect posts per page override in editor
chromium › posts-per-page.spec.js › Posts Per Page Override › should enforce max value from posts_per_page setting
chromium › query-presets.spec.js › Query Presets › should show query preset dropdown when presets are registered
chromium › query-presets.spec.js › Query Presets › should change post order when selecting alphabetical preset in editor
chromium › query-presets.spec.js › Query Presets › should change post order when selecting Z-A preset in editor
chromium › query-presets.spec.js › Query Presets › should apply query preset on frontend

The database is only reset once per run, so the post and its query loop showed up in the posts per page and query preset specs' listings.
@roborourke

Copy link
Copy Markdown
Collaborator

@tomjn so the other PR #31 is doing this more broadly, if you could test with that I’d like to get it shipped

@tomjn

tomjn commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favour of #31, which I've tested and does the same thing more broadly.

@tomjn tomjn closed this Sep 24, 2026
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