Template tests can result in incorrect post content being rendered
-
Hi
I’m encountering an issue where, when running a template test, the incorrect post content can be displayed on posts that should not be part of the test.
e.g. If I set up a template test aimed exclusively at a custom post type, content on standard pages is rendered incorrectly, usually meaning that posts render content from a different post completely when viewed in the front end.
This does not happen on all themes, but it is consistently happening in some.
I’ve narrowed the issue down to the logic in the
includes/hooks/experiments/templates/preview.phpfile, specifically around line 50:
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$query->the_post();
$links[ $key ] = get_permalink();
wp_reset_postdata();
}It seems this is somehow interfering with the default query (despite the call to
wp_reset_postdata()) which results in the template’s call tothe_content()referencing the wrong post. It appears thatthe_content()is rendering content from the post that this query returns, rather than the post that is being viewed.The correct behaviour happens if I either comment out those lines, or replace them with the
get_posts()equivalent:$posts = get_posts($args);
if (!empty($posts)) {
$links[$key] = get_permalink($posts[0]);
}Sorry I can’t offer full steps on how to recreate (the themes where I’m experiencing this are not ones I can share), but if it’d be possible to look into this (and possibly even fix by using the
get_posts()code I shared above), that’d be great, thank you.
The topic ‘Template tests can result in incorrect post content being rendered’ is closed to new replies.