Home Forums Community Forum Page breaking after event query

Page breaking after event query

Posted: December 8, 2020 at 9:35 am

Jill Davies

December 8, 2020 at 9:35 am


I’m using the below query to display 5 upcoming events.

<?php $atts = array(
'title' => NULL,
'limit' => 5,
'css_class' => NULL,
'show_expired' => FALSE,
'month' => NULL,
'category_slug' => NULL,
'order_by' => 'start_date',
'sort' => 'ASC'
// run the query
global $wp_query;
$wp_query = new EventEspresso\core\domain\services\wp_queries\EventListQuery( $atts );
while ($wp_query->have_posts()) : $wp_query->the_post();
$x = $post->EE_Event->first_datetime();?>
" aria-label="">
<p><?= $x->start_date('j M'); ?></p>
<p><?php echo the_title(); ?></p>

<?php endwhile;
wp_reset_postdata(); ?>

The events are displaying as expected but any code below the page is not displaying.

After removing while loop, the content displays below.

Can you please advise on how to retain the loop but display any code below?



  • Support Staff

December 8, 2020 at 2:54 pm

Hi there,

May I ask why you are overriding the global $wp_query object for this?

Don’t use global $wp_query; and then anywhere you are using $wp_query change it to $event_query.

Does it work for you then?

Jill Davies

December 9, 2020 at 2:53 am

Hi Tony,

Thank you for getting back to me.

I found the code somewhere (maybe stack overflow?)

Brilliant, thank you, that’s worked.

Thanks again.


  • Support Staff

December 9, 2020 at 4:32 am

Yeah, so the reason it wasn’t working is your overriding the global $wp_query which should at that point be the main query with the Event list query and then using it as a custom/secondary loop.

wp_reset_postdata(); resets the $post object back to the main query’s post but as you’ve not overwritten that, it doesn’t work 🙂

wp_reset_query() likely would have worked but you shouldn’t need to do that and should be using a custom variable as above.

So then when you reset above your resting the $post object, but $wp_query was still the ‘wrong’ query.

Anyway, just a quick explanation on what I think was happening, I’m glad the above worked for you 🙂

The support post ‘Page breaking after event query’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Do NOT follow this link or you will be banned from the site!