Opened 8 years ago
Closed 8 years ago
#37697 closed defect (bug) (fixed)
Strange behavior with thumbnails on preview in 4.6
Reported by: | jbenton | Owned by: | joemcgill |
---|---|---|---|
Milestone: | 4.6.1 | Priority: | high |
Severity: | normal | Version: | 4.6 |
Component: | Post Thumbnails | Keywords: | has-patch has-unit-tests commit fixed-major |
Focuses: | Cc: |
Description (last modified by )
Since upgrading my site to 4.6, we've had strange behavior regarding featured images on preview. Whenever a preview_nonce appears in a preview URL, an additional &_thumbnail_id=xxxxxxx appears. And at that point, all featured-image thumbnails on the page become that specific image — even when they appear in a different loop or are called through other means. @swissspidy says on Twitter this may be related to this change in 4.6: [38118]
Adding a wp_reset_postdata() does not seem to fix the problem. Screenshots and code available in this tweets in this Twitter thread:
Attachments (4)
Change History (21)
#2
@
8 years ago
- Keywords needs-patch needs-unit-tests added
- Owner set to joemcgill
- Priority changed from normal to high
- Status changed from new to accepted
#3
@
8 years ago
- Keywords needs-testing added; needs-patch needs-unit-tests removed
- Milestone changed from Awaiting Review to 4.6.1
#4
@
8 years ago
For those wanting to test this issue, you'll need to add something like the below code after the main loop on a custom page template and then preview a page using that template.
<?php $test_args = array( 'post_type' => 'post', 'posts_per_page' => 4, 'meta_query' => array( array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS' ), ), 'ignore_sticky_posts' => true, ); $stories = new WP_Query( $test_args ); while ( $stories->have_posts() ) { $stories->the_post(); if ( has_post_thumbnail() ) { the_post_thumbnail(); } } wp_reset_postdata(); ?>
This ticket was mentioned in Slack in #core-images by joemcgill. View the logs.
8 years ago
#6
@
8 years ago
- Keywords has-patch has-unit-tests added
There needs to be a unit test showing the effect of the additional $post_id != $_REQUEST['preview_id']
check.
In 37697.2.diff I added second unit test to demonstrate the fixed behaviour. I also added an empty( $_REQUEST['preview_id'] )
check to _wp_preview_post_thumbnail_filter()
and simplified the tests a bit.
- resets inside the tests should happen before the assertions. If an assertion fails, the following could won't be called
- simply doing
$GLOBALS['post'] = $old_post;
should be enough there
#7
@
8 years ago
37697.4.diff is similar to 37697.3.diff, but adds some additional cleanup to the unit tests to avoid potential test spillage.
#9
@
8 years ago
@jbenton do you perhaps have time to test this patch on your site to see if it properly fixes the bug?
This ticket was mentioned in Slack in #core-images by joemcgill. View the logs.
8 years ago
This ticket was mentioned in Slack in #core by jeremyfelt. View the logs.
8 years ago
#15
@
8 years ago
- Keywords fixed-major added
- Resolution fixed deleted
- Status changed from closed to reopened
Fixed in trunk. Reopening for minor.
#16
@
8 years ago
- Keywords needs-testing removed
I've reproduced this locally and [38433] resolves the issue. It might be nice to break the conditional up a bit in the future so that the properties of $post
are checked separately from the existence and values of _thumbnail_id
and preview_id
, but that's only a readability preference, not a functional concern.
Standard and multisite tests are passing.
@jbenton Thanks for the ticket. I noticed this bug at almost the same time you did, I think. The issue is that the new
_wp_preview_post_thumbnail_filter()
function fails to check if the$post_id
is from the post we're previewing, so it can affect any secondary loops on the preview page.37697.diff adds an additional check to make sure we're only affecting the main post object from the preview page and updates the unit test for this function so it includes the check as well.