Support

Home Forums Event Espresso Premium Get Event Start Date when looping through events…

Get Event Start Date when looping through events…

Posted: August 17, 2015 at 12:46 pm


Rob

August 17, 2015 at 12:46 pm

I had to write my own function to get the start date/time of events when looping through them in custom loops. There must be an easier (and more reliable) way?


function get_event_start_date( $EVT_ID ) {
  $times = EE_Registry::instance()->load_model('Datetime' )->get_all_event_dates( $EVT_ID );
  $times_obj = current($times);
  $test_obj = new ReflectionClass('EE_Datetime');
  $fields = $test_obj->getProperty('_fields');
  $fields->setAccessible(true);
  $event_start = $fields->getValue($times_obj)['DTT_EVT_start'];
  return $event_start;
}


Dean

August 18, 2015 at 6:51 am

Hi,

In the loop:

$x = $post->EE_Event->first_datetime(); 
echo $x->start_date();

What this does:

The $post object contains the Event object as well as the standard post data, so we use the first_datetime method in that object to extract the first datetime into a variable.

From that variable which now contains an instance of the EE_Datetime object, we can use the start_date method from the EE_Datetime object to display the start date

EE_Event – /wp-content/plugins/event-espresso-core-reg/core/db_classes/EE_Event.class.php

EE_Datetime – /wp-content/plugins/event-espresso-core-reg/core/db_classes/EE_Datetime.class.php


Rob

August 18, 2015 at 5:04 pm

That worked wonderfully. I had to do a bit of custom styling on portions of the date so I’ll post my code here in case someone else is trying to do the same.


    global $post;
    $x = $post->EE_Event->first_datetime(); 
    $event_start = strtotime($x->start_date());
    $day = date('j', $event_start);
    $month = date('M', $event_start);
    $year = date('Y', $event_start);
    $html .= '<span class="day">'.$day.'</span>';
    $html .= '<span class="month">'.$month.'</span>';
    $html .= '<span class="year">'.$year.'</span>';

The last thing I would like to understand is how to keep this custom query from returning events that have already ended. I looked through the code snippets but I wasn’t able to find anything similar.


// returns all events, even events which have ended
$args = array( 'post_type' => 'espresso_events', 'posts_per_page' => 3);
$alternative_coll = new WP_Query( $args );


Dean

August 19, 2015 at 5:11 am

Hi Rob,

A similar thing with this too.

Use the EE_Event object in the $post object, with the is_upcoming method found in the EE_Event class, e.g.:

global $post;
$check = $post->EE_Event->is_upcoming();

if( !$check )  {
continue;
}


Rob

August 20, 2015 at 6:36 am

Hey Dean- that seems like a very useful function to know. However, I’m not sure how to use it inside of the query for my custom loop. Perhaps something like…?


$upcomingEvents = new WP_Query( array(
  'post_type' => 'espresso_events',
  'posts_per_page' => 3,
  'meta_query' => array(
    'key' => 'is_upcoming',
    'value' => 'true',
    'compare' => '='
    )
  ) );


Dean

August 20, 2015 at 7:18 am

Hi,

No. It would need to be used in the loop, not the query (the “if ( have_posts() ) : while ( have_posts() ) : the_post();” bit).

https://codex.wordpress.org/The_Loop

Generally you would put it after the while section


Rob

August 20, 2015 at 7:48 am

I see- thanks!

Right now I need to retrieve a specified number (posts_per_page) of espresso_events which haven’t happened yet. Would this be the only way then:


$upcoming_events = array();
$allEvents = new WP_Query( array(
  'post_type' => 'espresso_events',
  'posts_per_page' => -1,  // to collect all events
  ) );
while($allEvents->have_posts()) { $allEvents->the_post();
  global $post;
  $check = $post->EE_Event->is_upcoming();
  if ($check) array_push($upcoming_events, $post);
}

To phrase it differently- is there anyway to do it using WP_Query?

Thanks for all of your help!


Dean

August 21, 2015 at 1:53 am

Hi,

Change

‘posts_per_page’ => -1,

to

‘posts_per_page’ => 10,

where 10 is the number of posts to display.


Rob

August 21, 2015 at 2:20 am

Thank you. Maybe I’m not asking my question very well, I apologize.

$post->EE_Event->is_upcoming(); takes place after querying. Instead of querying and then filtering events- I would really like to query for upcoming events beforehand (inside of WP_Query). Is it possible?


Dean

August 24, 2015 at 5:27 am

Hi,

I’m not sure if you can do it with WP_Query, but you could use the EE Query function, whihc replicates WP _Query but throws in some EE related stuff:

Found in — /wp-content/plugins/event-espresso-core-reg/shortcodes/espresso_events/EES_Espresso_Events.shortcode.php

Example — https://gist.github.com/joshfeck/e3c9540cd4ccc734e755

You can then use the show_expired parameter.

That’s the closest I can see to what you need.


Tony

  • Support Staff

August 24, 2015 at 5:38 am

Hi Rob,

Although you are free to use WP_Query directly, we recommend using the EE4 Models to query for your events as it does all the hard work for you. For info on the EE4 Models take a look here:

http://developer.eventespresso.com/docs/ee-model-objects-and-custom-post-types/

One of the easiest places to see this in action is the upcoming events widget within \event-espresso-core-reg\widgets\upcoming_events\EEW_Upcoming_Events.widget.php

For example, something like this:

//Setup where conditions
$where = array(
	'status' => 'publish',
	'Datetime.DTT_EVT_end' => array( '>=', EEM_Datetime::instance()->current_time_for_query( 'DTT_EVT_end' ) )
);

//Get events
$events = EE_Registry::instance()->load_model( 'Event' )->get_all( array(
	$where,
	'order_by' => 'Datetime.DTT_EVT_start',
	'order' => 'ASC',
	'group_by' => 'EVT_ID'
));

Will pull in only pull in active and upcoming events.


Tony

  • Support Staff

August 24, 2015 at 5:56 am

I’d opened this page before Dean posted, but the solution he posted works better than mine so I would recommend using that 🙂

EE_Event_List_Query does most of the work for you in this instance.

The support post ‘Get Event Start Date when looping through events…’ 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.

Support forum for Event Espresso 3 and Event Espresso 4.
Documentation for EE3 and EE4
Documentation for Event Espresso 3

Documentation for Event Espresso 4

Status: closed

Updated by  Tony 4 years, 3 months ago ago

Topic Tags

Notifications

This topic is: not resolved
Do NOT follow this link or you will be banned from the site!