Multiple WP_Query in blade template

I am using the controller on my front page to call 2 WP_Query for 2 different post types:

	<?php

namespace App\Controllers;
use Sober\Controller\Controller;
use WP_Query;

class FrontPage extends Controller
{
	public function get_latest() {
		$work_args = array(
			'post_type' => 'work',
			'posts_per_page' => 3,
		);
		$get_latest['work'] = new WP_Query( $work_args );

		$post_args = array(
			'post_type' => 'post',
			'posts_per_page' => 2,
		);
		$get_latest['posts'] = new WP_Query( $post_args );
		return $get_latest;
	}
}

I then call them like this i the blade template for the front-page:

@if( $get_latest['work']->have_posts() )
	<section id="latest-work">
		<h2>Latest Work</h2>
		@while ( $get_latest['work']->have_posts()) @php $get_latest['work']->the_post() @endphp
			@include('partials.content-work')
		@endwhile @php(wp_reset_postdata())
		<div class="dots">
			<a href="{{ home_url('/work/') }}" class="btn">View all work</a>
		</div>
	</section>
@endif

@if( $get_latest['posts']->have_posts() )
	<section id="latest-posts">
		<h2>Latest Blog Posts</h2>
		@while ( $get_latest['posts']->have_posts()) @php $get_latest['posts']->the_post() @endphp
			@include('partials.content-blog')
		@endwhile @php(wp_reset_postdata())
		<div class="dots">
			<a href="{{ home_url('/blog/') }}" class="btn">View all posts</a>
		</div>
	</section>
@endif

but all I get is an error saying unexpected endwhile

Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: syntax error, 
unexpected 'endwhile' 

Can anyone shed any light on why this is happening? I’m fairly new to both Sage and Blade templating.

I just thought I would share a work around that I have found for my problem.

It seems that multiple WP_Query on a template is an issue with Sage as a whole which is really putting me off using it again to be honest.

I have modified my code to merge the 2 WP_Query in the Controller instead and use an IF statement when I loop through them twice like this…

namespace App\Controllers;
use Sober\Controller\Controller;
use WP_Query;

class FrontPage extends Controller
{
	public function latest() {
		
	    $get_work = new WP_Query([
			'post_type' => 'work',
			'posts_per_page' => 3,
		]);
		
		$get_posts = new WP_Query([
			'post_type' => 'post',
			'posts_per_page' => 2,
		]);

		$latest = new WP_Query();
		$latest->posts = array_merge( $get_work->posts, $get_posts->posts );
		$latest->post_count = $get_work->post_count + $get_posts->post_count;

		return $latest;
	}
}

And then in the template:

@if( $latest->have_posts() )

	<section id="latest-work">
		<h2>Latest Work</h2>
		@while ( $latest->have_posts()) @php $latest->the_post() @endphp
			@if( get_post_type() == 'work')
				@include('partials.content')
			@endif
		@endwhile
		<div class="dots">
			<a href="{{ home_url('/work/') }}" class="btn">View all work</a>
		</div>
	</section>

	<section id="latest-posts">
		<h2>Latest Blog Posts</h2>
		@while ( $latest->have_posts()) @php $latest->the_post() @endphp
			@if( get_post_type() == 'post')
				@include('partials.content')
			@endif
		@endwhile
		<div class="dots">
			<a href="{{ home_url('/blog/') }}" class="btn">View all posts</a>
		</div>
	</section>

@endif

It is somewhat of a hack which I’m not happy about however it solved the issue for now.

1 Like

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