Support

Home Forums Event Espresso Premium Order My Events from Newest to Oldest

Order My Events from Newest to Oldest

Posted: July 5, 2018 at 7:20 am


ardiaful

July 5, 2018 at 7:20 am

I need to show My Events the newest first, now default events order being oldest first.


Josh

  • Support Staff

July 5, 2018 at 2:31 pm

Hi there,

This will involve adding some custom code to your template. For example, if you use the default loop-espresso_my_events-event_section.template.php template, you’ll copy that template to your theme, then immediately following the line:
if ($objects && reset($objects) instanceof EE_Event) : ?>
You’ll add:

<?php 
foreach ($objects as $object) :
    $object->start = '';
    if (! $object instanceof EE_Event) {
        continue;
    }
    $datetimes = array();
    $datetime = EEM_Datetime::instance()->get_primary_datetime_for_event( 
        $object->ID(), 
        true, 
        false
    );
    if(! $datetime instanceof EE_Datetime) {
        continue;
    }
    $object->start = $datetime->get_raw('DTT_EVT_start');
endforeach;
usort($objects, function($a, $b) {
    return strcmp($b->start, $a->start);
}); 
?>


ardiaful

July 6, 2018 at 1:47 am

I copied the code above as per your instructions starting on line 37 in loop-espresso_my_events-event_section.template.php but My Events is messed up and does not follow a descending date order:

<?php if ($objects && reset($objects) instanceof EE_Event) : ?>

<?php
foreach ($objects as $object) :
$object->start = '';
if (! $object instanceof EE_Event) {
continue;
}
$datetimes = array();
$datetime = EEM_Datetime::instance()->get_primary_datetime_for_event(
$object->ID(),
true,
false
);
if(! $datetime instanceof EE_Datetime) {
continue;
}
$object->start = $datetime->get_raw('DTT_EVT_start');
endforeach;
usort($objects, function($a, $b) {
return strcmp($b->start, $a->start);
});
?>


ardiaful

July 6, 2018 at 1:59 am

I add to my comment above that there is no match between the events in My Events section and the events listing in Registrations for this contact. I suppose both lists of events should be the same for the same user, however they are different and the “My Events” list does not include the events in the Registration list.


Josh

  • Support Staff

July 6, 2018 at 8:00 am

May I ask what’s the user ID and the Contact ID of the two you’re comparing? The reason I ask is because the two may not be connected.


ardiaful

July 6, 2018 at 8:47 am

I send info through form.


Josh

  • Support Staff

July 6, 2018 at 8:55 am

We haven’t received that specific information (the User ID & contact ID) in the form you sent.

In any case, please consider forgiving me for forgetting that you’ve already made other customizations on the display of dates within the content-espresso_my_events-event_section.template.php template.

So, in that template where you have this:

<?php $reg = reset($registrations);
$datetime = $reg->get_latest_related_datetime();
echo $datetime instanceof EE_Datetime ? 
    date_i18n(get_option('date_format') . ' ' . get_option('time_format'), 
    strtotime($datetime->start_date_and_time())) :
 ''; 
?>

This:
$datetime = $reg->get_latest_related_datetime();

should probably be changed to:
$datetime = $reg->get_earliest_related_datetime();

and that will un-“mess-up” the order of your events.’


ardiaful

July 6, 2018 at 9:09 am

Thanks for considering older customizations. However, $datetime = $reg->get_earliest_related_datetime(); breaks all my events list.


Josh

  • Support Staff

July 6, 2018 at 9:24 am

Please be sure to include the colon (;) at the end of the line, e.g.
$datetime = $reg->get_earliest_related_datetime();


ardiaful

July 6, 2018 at 9:34 am

Not working, still dates order is messy


Josh

  • Support Staff

July 6, 2018 at 9:55 am

Can you provide a screenshot with specific steps to replicate the issue?

Also, are there other customizations I should be aware of?


Josh

  • Support Staff

July 6, 2018 at 11:30 am

OK, it turns out at least some of your events have quite a number of datetimes that have start dates that span over many months.

Where that custom code I gave you would normally work OK is when an event has one datetime, or also datetimes that happen within a few days of each other. So the date order was not so much “messed up”, but rather going by the primary datetime of the event.

So the solution for you will to make the following adjustments to the custom code within loop-espresso_my_events-event_section.template.php:

<?php 
foreach ($objects as $object) :
    $object->start = '';
    if (! $object instanceof EE_Event) {
        continue;
    }
    $registrations = $object->get_many_related('Registration', array( array( 'ATT_ID' => $att_id ) ) );
    $reg = reset($registrations);
    $datetime = $reg->get_latest_related_datetime();
    if(! $datetime instanceof EE_Datetime) {
        continue;
    }
    $object->start = $datetime->get_raw('DTT_EVT_start');
endforeach;
usort($objects, function($a, $b) {
    return strcmp($b->start, $a->start);
}); 
?>

Please note that all of the extra queries to the database that this involves will likely slow the speed of the page load.


ardiaful

July 10, 2018 at 6:09 am

Thank you! the adjustment to the code works 100%

The support post ‘Order My Events from Newest to Oldest’ 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