Support

Home Forums Event Espresso Premium Script that hides Expired Classes also keeps many pages from showing in Search

Script that hides Expired Classes also keeps many pages from showing in Search

Posted: December 12, 2024 at 11:44 am

Viewing 6 reply threads


Portland Fashion Institute

December 12, 2024 at 11:44 am

Hey guys, So I’ve been trying to troublshoot this for ages. The Ivory Search on portlandfashioninstitute.com does not work (try to search for “Private Lessons”) But as soon as I remove this script, it works:


/**
* The purpose of this snippet is to filter the event archive (and event taxonomy archive) pages so that they exclude
* events that have tickets no longer on sale.
*
* NOTE: This query is only valid for Event Espresso 4.8+
*
* To Implement this code, add it to the bottom of your themes functions.php file, or add it to a site specific plugin.
*
* @param string $SQL
* @param WP_Query $wp_query
* @return string
*/
function de_ee_tweak_event_list_exclude_ticket_expired_events_where(string $SQL, WP_Query $wp_query): string
{
if (isEspressoEventsArchive($wp_query)) {
$SQL .= ' AND Ticket.TKT_end_date > "' . current_time('mysql', true) . '" AND Ticket.TKT_deleted=0';
}
return $SQL;
}
add_filter(
'posts_where',
'de_ee_tweak_event_list_exclude_ticket_expired_events_where',
15,
2
);

/**
* @param string $SQL
* @param WP_Query $wp_query
* @return string
* @throws EE_Error
* @throws ReflectionException
*/
function de_ee_tweak_event_list_exclude_ticket_expired_events_join(string $SQL, WP_Query $wp_query): string
{

if (isEspressoEventsArchive($wp_query)) {
$dates_table = EEM_Datetime::instance()->table();
$dates_table_pk = EEM_Datetime::instance()->primary_key_name();
$events_table = EEM_Event::instance()->table();
$events_table_pk = EEM_Event::instance()->primary_key_name();
$date_tickets_table = EEM_Datetime_Ticket::instance()->table();
$tickets_table = EEM_Ticket::instance()->table();
$tickets_table_pk = EEM_Ticket::instance()->primary_key_name();
if (
strpos($SQL, $dates_table) === false
&& ! $wp_query->is_espresso_event_archive
&& ! $wp_query->is_espresso_event_taxonomy
) {
$SQL .= " INNER JOIN $dates_table";
$SQL .= " ON ( $events_table.ID = $dates_table.$events_table_pk ) ";
}
if (
strpos($SQL, $date_tickets_table) === false
) {
$SQL .= " INNER JOIN $date_tickets_table AS Datetime_Ticket";
$SQL .= " ON ( Datetime_Ticket.DTT_ID = $dates_table.$dates_table_pk )";
}
if (
strpos($SQL, $tickets_table) === false
) {
$SQL .= " INNER JOIN $tickets_table AS Ticket";
$SQL .= " ON ( Datetime_Ticket.TKT_ID=Ticket.$tickets_table_pk )";
}
}
return $SQL;
}
add_filter(
'posts_join',
'de_ee_tweak_event_list_exclude_ticket_expired_events_join',
3,
2
);

This code prevents Expired Classes from showing up on our pages.

Any idea how I can do both – hide those classes AND get search to work? It’s the same thing no matter which search I use – default or custom plugin. It’s been driving us crazy…

Thanks,
Shelly


Tony

  • Support Staff

December 12, 2024 at 1:34 pm

Hi Shelly,

I would install this plugin:

https://en-gb.wordpress.org/plugins/query-monitor/

Then do a search on the site and on the results page look in the admin bar.

Hover over Query Monitor, then go to Database Queries

Set Caller to Main Query.

It should show something like this:

https://monosnap.com/file/koaRGeW6CYHWvpoaFWssauBgcVoJn7

Then copy the Query SQL and post it here please.


Portland Fashion Institute

December 12, 2024 at 3:55 pm

Ok.. Wasn’t 100% sure but here’s what was in the first column after selecting Main Query:

SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_h15xgqan21_posts.ID
FROM wp_h15xgqan21_posts
INNER JOIN wp_h15xgqan21_esp_datetime
ON ( wp_h15xgqan21_posts.ID = wp_h15xgqan21_esp_datetime.EVT_ID )
INNER JOIN wp_h15xgqan21_esp_datetime_ticket AS Datetime_Ticket
ON ( Datetime_Ticket.DTT_ID = wp_h15xgqan21_esp_datetime.DTT_ID )
INNER JOIN wp_h15xgqan21_esp_ticket AS Ticket
ON ( Datetime_Ticket.TKT_ID=Ticket.TKT_ID )
WHERE 1=1
AND ( ((wp_h15xgqan21_posts.post_title REGEXP ‘([[:<:]])private|private([[:>:]])’)
OR (wp_h15xgqan21_posts.post_content REGEXP ‘([[:<:]])private|private([[:>:]])’
AND wp_h15xgqan21_posts.post_password = ”)
OR (wp_h15xgqan21_posts.post_excerpt REGEXP ‘([[:<:]])private|private([[:>:]])’))
AND ((wp_h15xgqan21_posts.post_title REGEXP ‘([[:<:]])lessons|lessons([[:>:]])’)
OR (wp_h15xgqan21_posts.post_content REGEXP ‘([[:<:]])lessons|lessons([[:>:]])’
AND wp_h15xgqan21_posts.post_password = ”)
OR (wp_h15xgqan21_posts.post_excerpt REGEXP ‘([[:<:]])lessons|lessons([[:>:]])’)))
AND wp_h15xgqan21_posts.post_type IN (‘attachment’, ‘e-floating-buttons’, ‘e-landing-page’, ‘espresso_events’, ‘espresso_venues’, ‘instructor’, ‘page’, ‘post’)
AND ((wp_h15xgqan21_posts.post_status = ‘publish’
OR wp_h15xgqan21_posts.post_status = ‘inherit’))
AND Ticket.TKT_end_date > “2024-12-12 22:52:53”
AND Ticket.TKT_deleted=0
ORDER BY wp_h15xgqan21_posts.post_date DESC
LIMIT 0, 10


Portland Fashion Institute

December 12, 2024 at 3:56 pm

And here’s what’s in the second column:

WP_Query->get_posts()
wp-includes/class-wp-query.php:3357
WP_Query->query()
wp-includes/class-wp-query.php:3852
WP->query_posts()
wp-includes/class-wp.php:696
WP->main()
wp-includes/class-wp.php:816
wp()
wp-includes/functions.php:1336


Tony

  • Support Staff

December 13, 2024 at 5:41 am

I assume ‘Private lessons’ should end up here:

https://portlandfashioninstitute.com/private-lessons/

Looking at the query and testing the site, search does indeed work, for example if you search for ‘Dresses’ you end up here:

https://portlandfashioninstitute.com/?s=Dresses&id=29591

Thats displaying an EE event called ‘Dresses’.

But whats happening is the function is hooking into the query and adding the SQL to exclude results if there no tickets for that post. On the search query there’s multiple post types so search will ONLY work for EE events with upcoming tickets… any other search will return nothing (becuase nothing else has tickets).

Above the code you posted will be the function:

function isEspressoEventsArchive(WP_Query $wp_query): bool

At the bottom of that function will be:

&& ! $wp_query->is_singular;

Change that line so its:

&& ! $wp_query->is_singular
&& ! $wp_query->is_search;

Then try the above, what does it show?

Note – what should happen with the above is ‘Private Lessons’ (and any other search) should work again, but it does mean that searching will return events with expired tickets.


Portland Fashion Institute

December 27, 2024 at 1:49 pm

Great – will the shortcodes begin to show expired classes? Because when I removed the code entirely as a test, all of a sudden all our classes showed up on our pages (even though our short code says Show expired false).

I’m going to try this out and see what happens.

One Last Question – Do you supposed there is any way to wrap expired ticketed classes somehoW? It would be great if those classes when they show up in our search, they had a border with a note saying “currently not being offered” or something>?


Tony

  • Support Staff

December 28, 2024 at 8:32 am

Great – will the shortcodes begin to show expired classes?

The above change makes it excludes search queries, so no, the shortcodes should still show as they do now.

Because when I removed the code entirely as a test, all of a sudden all our classes showed up on our pages (even though our short code says Show expired false).

The current code works on the ‘normal’ queries (which includes those used by shortcodes), so removing/disabling the code will show events with expired tickets.

I’m going to try this out and see what happens.

Actually, I’ve just realised! We have this in core now.

Go to Event Espresso -> Events -> Templates.

Set ‘Display Events With Expired Tickets’ to No and save.

Remove the above snippet from the site and see if the event shortcodes show the correct events.

(Side note… Expired events are those where all of the datetimes within the event have expired, events where all of the tickets are expired are different)

One Last Question – Do you supposed there is any way to wrap expired ticketed classes somehoW? It would be great if those classes when they show up in our search, they had a border with a note saying “currently not being offered” or something>?

Interesting idea, not currently but I’ll look into this.

Viewing 6 reply threads

You must be logged in to reply to this support post. Sign In or Register for an Account

Event Espresso