'Nested WP_Query's

I get following error: PHP message: PHP Fatal error: Uncaught Error: Cannot use object of type WP_Query as array in wp-includes/class-wp-query.php:3456

Got error 'des/class-wp-query.php(3432): WP_Query->rewind_posts()\n#1 wp-includes/query.php(937): WP_Query->have_posts()\n#2 page.php(1)

i have following code:

<?php 
while(have_posts()) :
    the_post();
    get_template_part( 'template-parts/content', 'page');
    if(have_rows('content')):
        while (have_rows('content') ) : the_row();
            if(get_row_layout() == 'videos'):
                get_template_part('template-parts/videos');
            elseif(get_row_layout() == 'contact_block'):
                get_template_part('template-parts/contact_block');
            elseif(get_row_layout() == 'page_heading'):
                get_template_part('template-parts/page_heading');
            elseif(get_row_layout() == 'video'):
                get_template_part('template-parts/video_block');
            elseif(get_row_layout() == 'custom_posts'):
                get_template_part('template-parts/custom_posts');
            endif;
        endwhile;
    endif;
endwhile; // End of the loop.
?>

It's working fine but when i have a query in a template part i get this error.

So here is code of the custom_posts template part

<?php
$paged = (get_query_var('paged') ? get_query_var('paged') : 1);
$posts = new WP_Query([
    'post_type' => 'custom_post', 
    'posts_per_page' => get_sub_field('posts_per_page'), 
    'paged' => $paged
]);
if($posts->have_posts()):
    while($posts->have_posts()): $posts->the_post(); ?>
        <?php $class = 'col-md-6'.' col-lg-'.ceil(12/get_sub_field('col_amount')).(get_field('has_video') ? ' hasvideo' : ''); ?>
        <div class="<?=$class;?>">
            <div class="canhover fullimage<?=(get_field('has_video') ? ' hasvideo' : '');?>">
                <?php $image=get_field('image'); ?>
                <img src="<?=$image['url'];?>" alt="<?=$image['alt']; ?>" />
                <?php if(get_field('has_video')): ?>
                    <?php while(have_rows('content')): the_row();
                        if(get_row_layout() == 'video_block'):?>
                            <video controls>
                                <source src="<?=get_sub_field('video');?>" tye="video/mp4" />
                            </video>
                        <?php endif; 
            
                    endwhile;?>
                    <a class="playvideo" onclick="playVideo(this)" >&nbsp;</a>
                <?php endif; ?>
                <div class="text">
                    <p class="rest-text"><?=get_field('intro_text'); ?></p>
                </div>
            </div>
            <p class="permalink"><a href="<?php the_permalink(); ?>"><?=get_the_title(); ?></a></p>
            
        </div>
    <?php endwhile;
    wp_reset_postdata();
endif;
?>

if i remove while(have_posts()) : part it does work, but not sure if it's a correct way to solve this error.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source