Is it possible to retrieve a list of events using the WP_Query function so that I can write the page inside the loop? I know I could do this using eo_get_events but it would make it easier to duplicate code/styles (consistency) if I could pop everything into the loop.
As an extension to that, is it possible to pass arguments into WP_Query so that it only returns past or future events? I’m looking to create an upcoming events page and a past events page template.
Hope that makes sense 🙂
Paul
Absolutely! The eo_get_events()
, get_posts()
and WP_Query
all accept identical parameters*. eo_get_events()
just sets the post type to ‘event’ and ‘suppress_filters’ to false – so when using a native WordPress function / class you’ll need to set that.
Regarding showing future / past events, you can use the ‘event_start/end_before/after’ attributes (see docs). You can use ‘showpastevents’ but its preferred to set that to true, and use the ‘event_start/end_before/after’ attributes instead.
*They differ slightly in their documentation. For instance get_posts()
uses ‘numberposts’ to get limit the returned posts, but WP_Query()
uses ‘posts_per_page’. You can in fact use either with both of them, and eo_get_events()
too. eo_get_events()
is a wrapper for get_posts()
which uses WP_Query()
.
Stephen Harris
Thanks for your help. I was only able to get it working inside a standard loop by making the call directly through WP_Query:
$wp_query = new WP_Query( array('post_type' => 'event', 'suppress_filters' => false, 'posts_per_page' => 10) );
That’s worked great for creating a future events listing page but is there something I could pass in to retrieve expired events from a given events category?
Thanks.
Paul
Hi Paul,
If using get_posts()
or eo_get_events()
the ‘loop’ isn’t set up. So when using functions like eo_get_the_start()
you need to explicitly provide the post ID and the occurrence ID. If you want the loop architecture then you should use WP_Query
, but you should call wp_reset_query()
afterwards.
Try,
$event_query = new WP_Query( array(
'post_type' => 'event',
'suppress_filters' => false,
'posts_per_page' => 10,
'event_end_before' => 'today',
'showpastevents' => true
));
Dates are inclusive, so ‘yesterday’ may be more appropriate for you. See relative date formats. Also, see docs for eo_get_events()
as the a arguments are the same!)
Stephen Harris