Support

Home Forums Event Espresso Premium Sorting events by date

Sorting events by date

Posted: June 28, 2023 at 6:03 am

Viewing 11 reply threads


adijeff

June 28, 2023 at 6:03 am

I have an event listing which is an archive page https://aerosol-soc.com/events/ showing upcoming events. There is an event from 26-30 June that should be at the top, but is listed lower down, after 14th Nov, but before 16th Nov events.
I already have this in the functions:
https://gist.github.com/joshfeck/103efd299ce615188e978293a1254eeb
Thanks for any help.


Tony

  • Support Staff

June 28, 2023 at 3:09 pm

Hi there,

What are you using to query those events?

It doesn’t look like a standard WP archive loop.


adijeff

June 29, 2023 at 7:02 am

Hi Tony, It is a archive-espresso_events.php template file in the root of the theme with <?php if (have_posts()): ?> so I think it’s just the standard archive loop. There may be some functions altering the loop like this one: https://gist.github.com/joshfeck/103efd299ce615188e978293a1254eeb

Thanks


Tony

  • Support Staff

June 29, 2023 at 8:59 am

But there are multiple events__listing containers so there’s something more than just a single loop, right?

There may be some functions altering the loop like this one

Sure but within that snippet is a check to make sure it’s only running on the main query:

$query->is_main_query()

So is the above the main query or something custom?


adijeff

June 29, 2023 at 10:45 am

Thanks Tony. There are two events__listing containers, one is the main query, it starts:
<?php if (have_posts()): ?>

The second one sets out some $args, and then:

<?php $query = new EventEspresso\core\domain\services\wp_queries\EventListQuery( $args );
if ($query->have_posts()) : ?>


Tony

  • Support Staff

June 29, 2023 at 12:30 pm

Hmmm, ok.

If you install something to check the query on the page, does it show the custom order_by in use on that snippet?

For example, using Query Monitor or Debug Bar and inspecting the main query run on that page? I suspect that code isn’t hooking into that query.


adijeff

June 30, 2023 at 4:52 am

Thank Tony. I installed Query Monitor, and it’s showing a 1066 error (Not unique table/alias: ‘tl_esp_datetime’) due to FacetWP. If I disable FacetWP the order is correct, but I have to disable <?php echo facetwp_display( 'pager' ); ?> for the page to load.

Query Monitor shows me that this is the database query with the error, but I have no idea how to edit it:

SELECT tl_posts.ID, tl_esp_datetime.* , MIN( tl_esp_datetime.DTT_EVT_start ) as event_start_date , tl_esp_datetime.* , MIN( tl_esp_datetime.DTT_EVT_start ) as event_start_date , MIN( tl_esp_datetime.DTT_EVT_start ) as event_start_date
FROM tl_posts
INNER JOIN tl_postmeta
ON ( tl_posts.ID = tl_postmeta.post_id )
INNER JOIN tl_postmeta AS mt1
ON ( tl_posts.ID = mt1.post_id )
INNER JOIN tl_postmeta AS mt2
ON ( tl_posts.ID = mt2.post_id )
INNER JOIN tl_esp_datetime
ON ( tl_posts.ID = tl_esp_datetime.EVT_ID )
INNER JOIN tl_esp_datetime
ON ( tl_posts.ID = tl_esp_datetime.EVT_ID )
WHERE 1=1
AND ( tl_postmeta.meta_key = 'start_date'
AND ( ( mt1.meta_key = 'is_this_an_external_event'
AND mt1.meta_value != '1' )
AND ( mt2.meta_key = 'end_date'
AND CAST(mt2.meta_value AS DATE) >= '2023-06-30' ) ) )
AND ((tl_posts.post_type = 'espresso_events'
AND (tl_posts.post_status = 'publish'
OR tl_posts.post_status = 'acf-disabled'
OR tl_posts.post_status = 'cancelled'
OR tl_posts.post_status = 'postponed'
OR tl_posts.post_status = 'sold_out'
OR tl_posts.post_status = 'private')))
AND tl_esp_datetime.DTT_EVT_end > '2023-06-30 10:27:25'
AND tl_esp_datetime.DTT_EVT_end > '2023-06-30 10:27:25'
AND tl_esp_datetime.DTT_EVT_end > '2023-06-30 10:27:25'
GROUP BY tl_posts.ID
ORDER BY tl_posts.menu_order, event_start_date ASC

So I guess FacetWP is the culprit, and perhaps I should raise it with them. I need to prevent FacetWP interfering with this query, since FacetWP isn’t even used on this page!

Thanks for your help.


Tony

  • Support Staff

June 30, 2023 at 7:00 am

It looks like those filters are being applied to the query multiple times:

MIN( tl_esp_datetime.DTT_EVT_start ) as event_start_date , tl_esp_datetime.* , MIN( tl_esp_datetime.DTT_EVT_start ) as event_start_date , MIN( tl_esp_datetime.DTT_EVT_start ) as event_start_date

3 times?

INNER JOIN tl_esp_datetime
ON ( tl_posts.ID = tl_esp_datetime.EVT_ID )
INNER JOIN tl_esp_datetime
ON ( tl_posts.ID = tl_esp_datetime.EVT_ID )

2 times?

AND tl_esp_datetime.DTT_EVT_end > '2023-06-30 10:27:25'
AND tl_esp_datetime.DTT_EVT_end > '2023-06-30 10:27:25'
AND tl_esp_datetime.DTT_EVT_end > '2023-06-30 10:27:25'

3 times?

So I guess FacetWP is the culprit, and perhaps I should raise it with them. I need to prevent FacetWP interfering with this query, since FacetWP isn’t even used on this page!

It’s not? But above you said it is:

If I disable FacetWP the order is correct, but I have to disable for the page to load.

That isn’t mean trying to be nit picky, I’m trying to work out if there is a way to exclude those filters without breaking your page’s functionality.


adijeff

June 30, 2023 at 8:35 am

Yes, multiple times, but I have no idea how to change that.

However, I fixed it with help from FacetWP support and this snippet they sent for another event plugin https://gist.facetwp.com/gist/gist-ea65868cf1d314990872f85f6dfc90f1/

Sorry, when I said FacetWP wasn’t being used on the page, I meant there was no filtering of events on this page (which is what FacetWP does).

Thanks again for your help.


Tony

  • Support Staff

June 30, 2023 at 8:41 am

Oh, nice.

So you just changed the above to check for the espresso_events post type?

'espresso_events' == $query->get( 'post_type' )

Using that snippet stops the filter being run multiple times?


adijeff

June 30, 2023 at 9:24 am

Yes, that’s right. It works well, no database error, and events are ordered correctly.

Thanks again for your help.


Tony

  • Support Staff

June 30, 2023 at 9:34 am

Ah awesome, I’m glad you found a solution 🙂

Viewing 11 reply threads

The support post ‘Sorting events by 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.

Event Espresso