Support

Home Forums Event Espresso Premium Querying events on ACF custom field

Querying events on ACF custom field

Posted: November 18, 2015 at 2:45 pm


tesscoadmin

November 18, 2015 at 2:45 pm

I have created a custom post type called ‘Course’. Using the ACF plugin, I can associate a ‘Course’ with an EE Event. On each ‘Course’ page I would like to display all upcoming events associated with the ‘Course’ being displayed. This cannot be accomplished using shortcode. How can I use EE models, classes or template tags to build a query to return the results I need?


Josh

  • Support Staff

November 18, 2015 at 3:40 pm

Event Espresso events are also custom post types, stored in the WP posts table, so I don’t think you’ll need to use EE models, classes, or template tags to build the query. If you’re using ACF’s relationship fields, you can do a query like the one in their documentation:

http://www.advancedcustomfields.com/resources/relationship/


tesscoadmin

November 19, 2015 at 6:53 am

In my case the relationship is backward from this example. The custom field is on the EE event, but I’m viewing my ‘Course’ custom post type when I run the query. I’ve got something that works, but I want to refine it so I’m only looking for upcoming events in the query, rather than filtering the results for upcoming events afterwards. Here’s the code I have now:

$posts = get_posts(array(
	'orderby' => 'date',
	'order' => 'DESC',
	'posts_per_page' => 50,
	'post_type' => array('espresso_events'),
	'post_status' => 'publish',
	'meta_query' => array(
		array(
			'key' => 'related',
			'value' => $course_id,
			'compare' => '='
		)
	)
));
if ($posts) {
	foreach($posts as $post) {
		$event = EEM_Event::instance()->get_one_by_ID($post->ID);
		setup_postdata($post);
		if ($event->is_upcoming()) {
			// DISPLAY EVENT
		}
	}
}

How can I rewrite this so upcoming events are filtered in the get_posts() arguments rather that when I’m looping through the results?


Michael Nelson

  • Support Staff

November 19, 2015 at 2:57 pm

I’d rewrite it like so:


$events = EEM_Event::instance()->get_upcoming_events(
array(
 array(
  'Post_Meta.meta_key' => 'related',
  'Post_Meta.meta_value' => $course_id
  ),
 'order_by' => array(
  'Datetime.DTT_EVT_start' => 'ASC'
	  )
 )
);
foreach( $events as $event ) {
 //display event
}

This will get all upcoming events, ordering them by when the event’s datetime actually happens, not just when the event was published (I’m assuming that’s what you’re going to want. I could be wrong)
If you want info on model querying generally checkout http://developer.eventespresso.com/docs/model-querying/. Also looking at the classes in event-espresso-core/core/db_models/ might help


tesscoadmin

November 24, 2015 at 9:06 am

This is perfect. How do I modify this query to return the venue information as well?


Michael Nelson

  • Support Staff

November 24, 2015 at 11:32 am

Note that in the database, it’s actually possible to have multiple venues for an event.
To get and display the first venue for the event, you could do this:


foreach( $events as $event ) {
 $venues = $event->venues();
 if( ! is_array( $venues ) ) {
  continue;
 }
 $first_venue = reset( $venues );
 if( ! $first_venue instanceof EE_Venue ) {
  continue;
 }
 //use the venue
 echo $venue->name() . ' at ' . $venue->address();
}

Checkout the Venue class: https://github.com/eventespresso/event-espresso-core/blob/master/core/db_classes/EE_Venue.class.php

The support post ‘Querying events on ACF custom field’ 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.

Event Espresso