Support

Home Forums Event Espresso Premium Display all Events associated with a Venue – Displayed on Single Venue Page

Display all Events associated with a Venue – Displayed on Single Venue Page

Posted: August 14, 2014 at 4:16 pm

Viewing 12 reply threads


James Kramer

August 14, 2014 at 4:16 pm

I need to display all events that are associated with a venue. What is the best way to do this?

Related to this question, is there a way to get an Event object by ID (which would include all event details)?

My current solution uses a secondary advanced custom field (ACF) to build a relationship between the Event and Venue CPT. I use a reverse relationship to create an array of Events and then a FOREACH statement to select only the Events which are associated with the current page ID (the venue page ID).

This solution works great for getting the basic CPT information from the events associated with the Venue. There are two issues with this method. 1) I have to have a duplicate Venue field on the Event edit page. 2) I can’t get any other information except standard WP info about the event. I need Event date, price, availability, etc.

Because the above solution doesn’t use Wp_Query I can’t use the template tags. I could use all of my existing code if there was a way to query events based on ID and get the full Event object returned.

Alternatively, is there a way to load the Event helper onto the single Venue page outside the WP Loop?

You can see the dev page where this particular code is being used here (it’s in active development so it may change and/or be broken when you visit).

The following code is listed after the primary Loop for that page which displays the Venue details.

<?php 
$events = get_posts(array(
	'post_type' => 'espresso_events',
	'meta_query' => array(
		array(
			'key' => 'venue', // name of custom field
			'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
			'compare' => 'LIKE'
		)
	)
)); ?>

<?php if( $events ): ?>
	<?php foreach( $events as $event ): ?>
	
		<!-- Do loop stuff -->

	<?php endforeach; ?>


James Kramer

August 19, 2014 at 11:22 am

How can I list the events associated with a venue, displayed on the single venue page, without a shortcode?


Sidney Harrell

August 19, 2014 at 1:51 pm

In the same way that you grabbed the EE_Event object in your other post, you’ll need to grab the EE_Venue object, then use it’s events method: http://code.eventespresso.com/classes/EE_Venue.html#method_events. Here’s the get_venue method: http://code.eventespresso.com/classes/EEH_Venue_View.html#method_get_venue. Note that you may need to go into the code and change that method to public. (it’s been fixed in 4.4)


James Kramer

August 19, 2014 at 5:37 pm

Thanks Sidney, it sounds like that will work. Can you give me an example of how I would use that?


James Kramer

August 21, 2014 at 10:07 am

To be more clear, how would I adjust the following code so it does what you recommend?

function espresso_clean_event_status() {
    global $post;
    $EVT_ID = $post->ID;
    $event = EEH_Event_View::get_event( $EVT_ID );
    $status = $event instanceof EE_Event ? $event->pretty_active_status( FALSE ) : 'inactive';
    $status_sans_tags = wp_strip_all_tags($status);
    return $status_sans_tags;
}

Thanks so much!


James Kramer

August 21, 2014 at 7:36 pm

I got this working with your recommendation. Thank you!

On the single venue page I already had the venue object so I just called the events() method on it to display all events associated with that venue. It works great except…

The events list is not sorted by start date. What’s the best way to do that? You can see this in action here.


Josh

  • Support Staff

August 22, 2014 at 1:52 pm

Hi James,

They look to be sorted by ascending date when I view the page. Did you get this worked out?


James Kramer

August 22, 2014 at 1:55 pm

Thanks for taking a look, Josh.

I was able to get it working with the following code:

<?php
// Use events() method on the venue object to get all events for this venue:
$venues_events = $venue->events();

// Check if there are any events:
if( $venues_events ): ?>

<?php
// attach the start date, as a simple string, to each event:
foreach( $venues_events as $venue_event ) {
	$venue_event->start_date = $venue_event->primary_datetime()->start_date_and_time('YmdGi', ' ');
}

// Re-order events by start date string.
function cmp($a, $b)
{
    return strcmp($a->start_date, $b->start_date);
}
usort($venues_events, "cmp");
?>

<!-- Begin looping through events -->
<?php foreach( $venues_events as $event ): ?>

	<!-- loop stuff -->
	
	<?php// Is there an easier way to get the ID? I made _fields public to get it.
		$event->ID = $event->_fields["EVT_ID"];
		// Add event status
		$event->status = espresso_clean_event_status($event->ID); ?>


Dean

August 25, 2014 at 1:36 am

Hi,

Thanks for sharing the code!


James Kramer

September 2, 2014 at 10:47 am

Turns out this broke with multiple events. Is there a $query_params I can send to the events() method that will sort them by start date?


James Kramer

September 2, 2014 at 11:05 am

It appears that the Primary Datetime method returns null when multiple events are called.

[92]=>
  object(EE_Event)#930 (10) {
    ["_Primary_Datetime"]=>
    NULL

You can see the var_dump( $venues_events ) here.


James Kramer

September 3, 2014 at 12:14 pm

Alright, I found a solution. It’s dirty but I have to take this site live in a couple days so it’ll have to work until I hear back from support.

It looks like the error was taking place because the events() method calls all events, even those in the trash. Apparently, primary_datetime()->start_date_and_time() doesn’t work on events in the trash.

Instead of trying to change the query I just use if statements to ignore the trashed events. However, it hinges on the _fields array which I had to change from private/protected to public. I’ll have to go back and make that public every time I update the plugin. I’m not too happy about that and I’d love some feedback if there’s a better way to do this.

I modified the above code to only call the start_date_and_time() method if the event has a status of “publish”:

// attach the start date, as a simple string, to each event:
foreach( $venues_events as $venue_event ) {
	
	// Do not try to attach the start date of an event that is not published.
	if( $venue_event->_fields["status"] === "publish" ) {

		$venue_event->start_date = $venue_event->primary_datetime()->start_date_and_time('YmdGi', ' ');
		
	}

}

And then in my loop I only display events if their status is also “publish”.

<!-- Begin looping through events -->
<?php foreach( $venues_events as $event ): ?>

	<!-- Only display events that are published (otherwise it will display events that have been trashed) -->
	<?php if( $event->_fields["status"] === "publish" ): ?>

	<article class="list-event">

None of this would be necessary if two things were assumed when I call the events() method.
1. I only want published events.
2. I want them to be in order of start date.

I think this should be default behavior.


Josh

  • Support Staff

September 5, 2014 at 2:50 pm

Hi James,

I think I follow what you’re trying to. This will also do the job:

$events = 
EEM_Event::instance()->get_all(array(
	array( 
		'Venue.VNU_ID'=>$post->ID
	), 
	'order_by'=>'Datetime.DTT_EVT_start','order'=>'DESC'
) );

foreach($events as $event){
	// loop here
}
Viewing 12 reply threads

The support post ‘Display all Events associated with a Venue – Displayed on Single Venue Page’ 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