WP_Query infinite loop, page doesn't load

Hello! I’m not sure if I’m doing something wrong but I’ve encountered an issue where when I loop through a custom WP_Query, it creates an infinite loop and the page never loads.

Here’s the gist of what I’m trying to achieve;
I have a static front-page where I want to display only 6 posts from the blog. To do so, I’ve made a blogLoop method in the FrontPage controller with the following contents:

public function blogLoop()
{
    $query = new \WP_Query([
        'post_type' => 'post',
        'posts_per_page' => 6
    ]);

    return $query;
}

Then, in my front-page.blade.php template I have the following code to loop through the posts:

@if($blog_loop->have_posts())
    @while ($blog_loop->have_posts()) @php the_post() @endphp
        @include('partials.content-'.get_post_type())
    @endwhile
@endif

Now, when adding the @while loop, this creates the infinite page load. I’ve tried inlining the WP_Query in the template itself but I get the same result.

For anyone else encountering this, I just had to use @php $blog_loop->the_post(); @endphp instead and that fixed it.

This topic was automatically closed after 42 days. New replies are no longer allowed.