• Resolved Webmazing

    (@webmazing)


    Hi, first of all great plugin! you are a hero!

    We have some problems with the Dynamic preview. We have multiple ACF layouts and they work fine. But the wp_query blocks not 100%. In the code we do set up the query and with an ACF field we get the amount of posts. But the layout is rendered twice. This happens on all blocks with wp_query.

    Can you help us out?

    Thanks

    Cheers,

    Mitchell

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello!

    I’m glad to hear that you enjoy ACF Extended ??

    I’m not sure to completely understand your problem. In your flexible content, you add a layout (which has a WP Query), and it’s added twice in the administration?

    Can you please give me some more informations?

    Regards.

    Thread Starter Webmazing

    (@webmazing)

    Hi,

    in my render template I use wp_query. The content of that query is added twice in 1 layout screen.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    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.

    Thread Starter Webmazing

    (@webmazing)

    Nice! works like a charm! thanks for the quick and good reply.

    I will get it out of my functions.php when you release an update. So we can close this one. thanks!

    5 STARS ??

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hey,

    Thank you ??

    Regards.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    BTW, if you have slack, you can join us in the public ACF community. Here is the Slack invitation (I’m often connected there, to answer questions etc…)

    Have a nice day

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Double query results at Dynamic preview’ is closed to new replies.