Posted: 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?
|
|
Hi, In the loop:
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 |
|
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.
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.
|
|
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.:
|
|
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…?
|
|
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 |
|
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:
To phrase it differently- is there anyway to do it using WP_Query? Thanks for all of your help! |
|
Hi, Change ‘posts_per_page’ => -1, to ‘posts_per_page’ => 10, where 10 is the number of posts to display. |
|
Thank you. Maybe I’m not asking my question very well, I apologize.
|
|
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. |
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. |
|
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.