Skip to content

fix: Prevent PostsIterator from resetting the post global in the admin#3117

Draft
gchtr wants to merge 1 commit into2.xfrom
2.x-post-global-in-admin
Draft

fix: Prevent PostsIterator from resetting the post global in the admin#3117
gchtr wants to merge 1 commit into2.xfrom
2.x-post-global-in-admin

Conversation

@gchtr
Copy link
Copy Markdown
Member

@gchtr gchtr commented Jun 5, 2025

Issue

When calling Timber::get_posts() in the admin, Timber will call $post->setup() for all posts, which will cause the $post global to be overwritten. This intended when looping over posts in the frontend, but it shouldn’t be done in the WordPress admin. Because there it can mess up your posts.

For example, you could open a post in the block editor and the content of a different post is loaded in there.

Solution

Add a check to only run the setup() in the frontend.

Impact

Fewer side-effects.

Usage Changes

None.

Considerations

I’m not sure whether the template_redirect hook is the right check. That’s why I’m opening this pull request as a draft first.

We surely need to check how this would different ways of accessing the post content, e.g. through the REST API.

Testing

Not yet.

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.

1 participant