Support

Home Forums Event Espresso Premium How do we hide events that have sold out?

How do we hide events that have sold out?

Posted: December 10, 2020 at 5:20 am


codingforsail

December 10, 2020 at 5:20 am

Hi,

We want to hide sold out events from the events list to encourage people to book those which are available or request a custom event.

I found this:
https://eventespresso.com/topic/event-table-view-hide-sold-out-events/

But we are not using the Event Table addon, as we have Multi-Event Registration so I can’t find the particular template mentioned, nor a similar one.

Kind regards,

Anita


Tony

  • Support Staff

December 10, 2020 at 5:52 am

Hi Anita,

You can use a snippet like this:

https://gist.github.com/Pebblo/285340c950796e16eae8ba1d2e520513

That should remove sold out events from the front end event lists.


codingforsail

December 10, 2020 at 9:09 am

Thanks Tony.

I added that to site-specific plugin; so added the add_filter to the class constructor, and the function as given later in the class (copied and pasted).

But broke the site. Pretty much any page gives a 404 error with the new filter enabled. The pages return when I comment it out.

Site is using WP 5.4.2


Tony

  • Support Staff

December 10, 2020 at 9:28 am

Can you post the code you are using, please? Add it to a Gist or Pastebin and post the link here.

The above snippet is working fine for me.


codingforsail

December 10, 2020 at 10:11 am

I think it’s here: https://pastebin.com/e9vFUZQX


Tony

  • Support Staff

December 10, 2020 at 1:58 pm

When you’re working within a class, you need to declare where the function comes from in your add_filter()

So on line 86, this:

add_filter( 'posts_where', 'tw_ee_tweak_event_list_exclude_sold_out_events_where', 15, 2 );

Needs to be:

add_filter( 'posts_where', array($this, 'tw_ee_tweak_event_list_exclude_sold_out_events_where'), 15, 2 );

Does it work then?


codingforsail

December 11, 2020 at 5:36 am

Aggh, stupid mistake. Sorry.

Yes it worked once I’d moved the closing bracket. Also tweaked to remove events that are password-protected:

# Exclude sold out and password-protected events from EE event lists
add_filter( 'posts_where', array($this,'tw_ee_event_list_exclude_sold_out_and_pwprotected_events_where'), 15, 2 );
/**
* Function to exclude sold out and password-protected events from EE Event lists
*/
function tw_ee_event_list_exclude_sold_out_and_pwprotected_events_where( $SQL, WP_Query $wp_query ) 
{
    if ( isset( $wp_query->query_vars['post_type'] ) && ( $wp_query->query_vars['post_type'] == 'espresso_events'  || 
			 ( is_array( $wp_query->query_vars['post_type'] ) && in_array( 'espresso_events', $wp_query->query_vars['post_type'] )
			 ) ) && ! $wp_query->is_singular
			) 
		{
			global $wpdb;
			$SQL .= "AND {$wpdb->prefix}posts.post_password = '' ";
			$SQL .= "AND {$wpdb->prefix}posts.post_status != 'sold_out' ";
		}
		return $SQL;
	}

Pasted in case useful to others.


Tony

  • Support Staff

December 11, 2020 at 6:20 am

Yes it worked once I’d moved the closing bracket.

Lol, oops, nice spot. My mistake this time and I’ve fixed it in my post in case anyone copies it.

Thanks for sharing your updated code and I’m glad it’s now working for you.

The support post ‘How do we hide events that have sold out?’ 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.
Documentation for EE3 and EE4
Documentation for Event Espresso 3 Documentation for Event Espresso 4

Status: closed

Updated by  Tony 2 months, 2 weeks ago ago

Topic Tags

Notifications

This topic is: resolved
Do NOT follow this link or you will be banned from the site!