Hello,
Yes, this is a 8 years old WP bug, described in this ticket: https://core.trac.www.ads-software.com/ticket/18408. WP_Query in the admin area leads to duplicate results render.
This issue also caused problem to Gutenberg: https://github.com/WordPress/gutenberg/issues/7468
It can be resolved by doing a backup of the original $post
and reassign it after the query.
Here is a usage example inside your layout template.php
file:
<?php
// Backup post
global $post;
$_post = $post;
$query = new WP_Query(array(
'post_type' => 'page'
));
?>
<?php if($query->have_posts()): ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<?php the_title(); ?><br/>
<?php endwhile; ?>
<?php endif; ?>
<?php
// Restore original post
$post = $_post;
?>
I’ll add a fix at the Flexible Content Field level in the next plugin’s update, using the same method as Gutenberg.
Meanwhile, you can fix it by adding the following code in your functions.php
. Then you can use WP_Query normally in any layout:
/*
* Flexible Content Preview: Fix WP_Query
* See: https://core.trac.www.ads-software.com/ticket/18408
* See: https://www.ads-software.com/support/topic/double-query-results-at-dynamic-preview/
*/
add_action('acfe/flexible/preview', 'flexible_preview_query_fix_before', 98);
function flexible_preview_query_fix_before(){
// Backup original post
global $post;
$_post = $post;
}
add_action('acfe/flexible/preview', 'flexible_preview_query_fix_after', 100);
function flexible_preview_query_fix_after(){
// Restore original post
global $post, $_post;
$post = $_post;
}
I’ll tell you when the update will be out, so you can remove this code ??
Regards.