Im trying to use the EE_Event_List_Query to print out the events by their event start date instead of using WP_query to print events by their creation date. Im open to using either though.
Ive tried to model my loop after Josh’s example, but im having an issue accessing the post object. Heres what ive got so far:
My end goal is to return the events start date in a certain format (August 2016). I had this working before, but it was sorting by event creation date even though it was returning the event start date:
Not sure how I didnt notice the missing variable there, thank you. So heres what my function looks like now and im still getting the “Fatal error on a non-object” error:
function events_by_month($taxonomySlug) {
// array to use for results
$months = array();
// get posts from WP
$eventAtts = array(
'limit' => -1,
'order_by' => 'start_date',
'sort' => 'ASC',
'category_slug' => $taxonomySlug,
'post_status' => 'publish',
'show_expired' => FALSE
);
// loop through posts, populating $months arrays
global $events_query;
$events_query = new EE_Event_List_Query( $eventAtts );
if ( $events_query->have_posts()) : while ( $events_query->have_posts()) : $events_query->the_post();
$event_obj = $post->EE_Event;
$date_times = $event_obj->first_datetime();
// $event_day = date('j', $event_start);
// $event_month = date('F', $event_start);
// $event_year = date('Y', $event_start);
// $x = $post->EE_Event->first_datetime();
$months[date('F Y', strtotime( $date_times->start_date() ))][] = $post;
endwhile;
endif;
// reverse sort by year
krsort($months);
return $months;
}
the second post in this thread shows a near working example of what im trying to accomplish, and I did get the event object using the same syntax. The only issue with that function was that it was returning an array that was sorted by the event(post) creation date instead of the event’s start date.
Ill look through the couple examples you’ve posted and see what i can put together, but if you can find a way to get the event object in the event query that would be ideal.
got the function working now! thanks for all the help!
function events_by_month() {
// array to use for results
$months = array();
// set up show expired = false
$date_format = get_option( 'date_format' );
$time_format = get_option( 'time_format' );
$args = array(
'limit' => -1,
'sort' => 'ASC',
'order_by' => 'start_date',
'category_slug' => 'ecap-events',
'show_expired' => FALSE,
);
$loop = new EE_Event_List_Query( $args );
if( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post();
$eventPost = get_post( get_the_id() );
$datetimes = EEM_Datetime::instance()->get_datetimes_for_event_ordered_by_start_time( $eventPost->ID, false, false, 1 );
foreach ( $datetimes as $datetime ):
$months[date('F Y', strtotime( $datetime->start_date() ))][] = $eventPost;
endforeach;
endwhile;
endif;
return $months;
wp_reset_postdata();
}
Viewing 6 reply threads
The support post ‘Using EE_Event_List_Query to sort events by start date instead of post date’ 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.