Make WordPress Core

Opened 14 months ago

Closed 8 weeks ago

#62975 closed defect (bug) (fixed)

Twenty Twenty-Five: Exclude sticky posts when offset is used

Reported by: poena's profile poena Owned by: joedolson's profile joedolson
Milestone: 7.0 Priority: normal
Severity: normal Version:
Component: Bundled Theme Keywords: has-patch has-screenshots commit
Focuses: Cc:

Description (last modified by poena)

In Twenty Twenty-Five, there are several templates that use query loop blocks with offsets.

Sticky posts are not hidden by default when offset is used, which means that if a website has sticky posts, they show more than once when the templates are used.

The repeated content is not desired because it is not a good experience for visitors.

See this support request:
https://wordpress.org/support/topic/posts-loop-broken-when-complex-blog-template-used/

This can be solved by updating the query loops that have offsets to also ignore sticky posts.
The "ignore" option is planned to be added in WordPress 6.8. See https://core.trac.wordpress.org/ticket/62908

Another alternative would be to use "exclude" sticky posts, then the sticky would not be repeated, but it would not be shown at all.


Users can also remove the sticky status from their posts. But it is not so easy for users to discover that they can solve the problem that way.

Attachments (2)

News-blog-with-sidebar-6.7.png (681.8 KB) - added by sabernhardt 6 months ago.
News blog with sidebar pattern in WordPress 6.7, with "sticky":"ignore" from the patch
tt5-exclude-sticky-posts-when-offset-used.diff (1.0 KB) - added by sachinrajcp123 4 months ago.

Download all attachments as: .zip

Change History (22)

#1 @poena
14 months ago

  • Description modified (diff)

#3 @abcd95
14 months ago

Hi @poena, Thanks for bringing this to Trac!

I have been thinking about this and maybe we can completely remove sticky posts from offset queries, or add a custom filter to dynamically exclude sticky posts only when offset is used.

The former approach is simpler but more restrictive, while the latter offers more flexibility. What are your thoughts on this?

#4 follow-up: @poena
14 months ago

Did you mean that the change should be made in the query loop block itself and not in the theme?

I think the expectation is that the behaviour of the query loop should match wp query, where the sticky posts are not removed by the offset.

#5 in reply to: ↑ 4 @abcd95
14 months ago

Replying to poena:

Did you mean that the change should be made in the query loop block itself and not in the theme?

Actually, that makes more sense. Perhaps we could restructure affected templates to either split the query (sticky posts + offset posts) or provide alternative templates without offsets?

#6 @rinkalpagdar
14 months ago

@poena Can you please provide testing instructions if possible? Thanks!

#7 @poena
14 months ago

The testing instruction are:
Create a few sticky posts.
Go to Appearance > Editor > Templates
Open a template that uses query loops and has design alternatives.
Select a design that has multiple loops.
Save. View the template on the front of the site.

#8 @rinkalpagdar
14 months ago

Hello @poena Thanks for the quick repose. I can reproduce the issue. Also, I have tested it with various themes and learned that it's not an issue with the query loop block but with the theme template. Am I right? Please correct me if I am wrong

#9 @poena
13 months ago

  • Milestone changed from Awaiting Review to 6.9

Yes the issue is in the theme templates / patterns. The designs with multiple query loop needs have sticky set to exclude.

#10 @freibergergarcia
12 months ago

One alternative would be to apply a filter that only fires when the query loop block is used.

For that, you could leverage query_loop_block_query_vars (https://developer.wordpress.org/reference/hooks/query_loop_block_query_vars/), for example:

<?php
/**
 * Don't prepend sticky posts if a Query Loop block uses an offset.
 */
add_filter(
        'query_loop_block_query_vars',
        function ( $query_vars, $block ) {
                if ( ! empty( $query_vars['offset'] ) ) {
                        $query_vars['ignore_sticky_posts'] = true;
                }

                return $query_vars;
        },
        10,
        2
);

Obs: that would only apply when offset is > 0.

We would need to validate if that will conflict with your comment https://core.trac.wordpress.org/ticket/62975#comment:2.

Did https://github.com/WordPress/wordpress-develop/pull/8265/ fix this?
Then you can ignore this.

Last edited 12 months ago by freibergergarcia (previous) (diff)

This ticket was mentioned in PR #9471 on WordPress/wordpress-develop by @akshat2802.


8 months ago
#11

  • Keywords has-patch added

PR for : https://core.trac.wordpress.org/ticket/62975

#### Summary

This PR updates bundled theme templates (e.g., Twenty Twenty-Five, Twenty Twenty-Four) that use Query Loop blocks with an offset to set the default to ignore sticky posts.

#### Why
When a Query Loop with an offset does not ignore sticky posts, sticky posts can appear more than once in their designated sticky slot and again in the offset loop. This leads to duplicate content and a suboptimal reading experience.

#12 @akshat2802
8 months ago

This is one possible approach to improving the experience in bundled theme templates (e.g., Twenty Twenty-Five), where Query Loop blocks with an offset may display sticky posts more than once.

When sticky posts are not ignored, they can appear both in their featured position and again later in the loop, which can feel repetitive for visitors.

This PR https://github.com/WordPress/wordpress-develop/pull/9471 updates affected templates to set "sticky": "ignore" by default when using offsets. If a site owner or administrator prefers a different behaviour, they can easily adjust the Query Loop settings in the Site Editor to enable sticky posts or select another option.

The change also aligns with the new ignore option introduced in WordPress 6.8

#13 @mindctrl
6 months ago

  • Keywords needs-testing added

@sabernhardt
6 months ago

News blog with sidebar pattern in WordPress 6.7, with "sticky":"ignore" from the patch

#14 @sabernhardt
6 months ago

  • Milestone changed from 6.9 to 7.0

The "sticky":"ignore" setting in the patch did not give me errors in WordPress 6.7.0, so that might be a viable option. The <select> dropdown shows "Include" there, but the sticky post (published after "4") is not included.

I'm moving this to 7.0 for more testing. If you think it is ready to commit within the next week, feel free to put it back to 6.9.

#15 @ozgursar
3 months ago

  • Keywords has-screenshots added; needs-testing removed

Reproduction and Test Report

Description

✅ This report validates whether the issue can be reproduced.
✅ This report validates whether the indicated patch works as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/9471

Environment

  • WordPress: 7.0-alpha-61215-src
  • PHP: 8.2.29
  • Server: nginx/1.29.4
  • Database: mysqli (Server: 8.4.7 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 143.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.4
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.1

Steps to Reproduce

  1. Add 10 sticky posts
  2. Add 10 regular posts
  3. Activate Twenty Twenty-Five theme
  4. Appearance > Editor > Templates > Blog Home
  5. Choose Design: News blog home (or any other design that has multiple query loops)
  6. View the homepage

Actual Results

  1. ✅ Error condition occurs (reproduced).
  2. ✅ Issue resolved with patch.

Supplemental Artifacts

Before:
https://files.catbox.moe/db14wo.png

After:
https://files.catbox.moe/2iu3uw.png

Last edited 3 months ago by ozgursar (previous) (diff)

@joedolson commented on PR #9471:


2 months ago
#16

I looked at this PR, and while I think it functionally does what is needed, I think it may be doing more than is desired - the goal was to exclude sticky posts in templates that have multiple query loops, but it looks to me like this is excluding sticky posts in *all* templates. Is that what's actually desired, @poena?

I don't think this will cause any problems with versions before 6.8. Based on @sabernhardt's testing notes, I looked at the code, and I don't see any reason this would throw an error on earlier versions. It won't *work* on earlier versions, but that's not a concern - it would just be the same as it is now.

#17 @joedolson
2 months ago

  • Owner set to joedolson
  • Status changed from new to accepted

#18 @poena
2 months ago

Hi
I agree that only some queries should have stickies set to ignore.

These patterns have multiple query loops and will show duplicate content if sticky posts are included.

News blog with sidebar
template-home-with-sidebar-news-blog.php

I would expect the sidebar to show all posts including the sticky,
but the remaining queries to have sticky set to ignore. Open for other suggestions of course.

News blog home
template-home-news-blog.php

I would expect all queries to have sticky set to ignore.

News blog with featured posts grid
template-home-posts-grid-news-blog.php

I would expect all queries to have sticky set to ignore.
If the first query at the top of the page does not have sticky set to ignore, that section will show more than one post.


Portfolio homepage
page-portfolio-home.php

This can be tested on a page, not the home.
I would expect all queries to have sticky set to ignore.

I'm leaving out the default search results template which has two queries,
it uses a pattern called "More posts" above the footer. I have not seen any user reports about this being confusing/unexpected/unwanted.

@joedolson commented on PR #9471:


2 months ago
#19

Hi, @AKSHAT2802!

Please see the below comment from @carolinan concerning the changes that need to be made on this PR - thank!

https://core.trac.wordpress.org/ticket/62975#comment:18

#20 @joedolson
8 weeks ago

  • Keywords commit added

While the prior test technically tested a different patch, the principle is unchanged, and I consider the test to still be valid; it just was testing cases in addition to what the current patch actually covers.

Marking for commit; self tested to confirm.

#21 @joedolson
8 weeks ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 61720:

Twenty Twenty Five: Exclude sticky posts with query offsets.

Fix templates in Twenty Twenty Five that have multiple query posts with offsets to prevent displaying sticky posts. Sticky posts being enabled on multiple queries results in repeated content in each query.

Add "sticky":"ignore" to query block parameters.

Props poena, sabernhardt, abcd95, rinkalpagdar, freibergergarcia, akshat2802, mindctrl, ozgursar, joedolson.
Fixes #62975.

Note: See TracTickets for help on using tickets.