The Events Calendar find the available number of tickets for an event

This isn’t a particularly clever bit of code, but I’m putting it here as I struggled to find a solution, so hopefully this might help someone. Or me if I need it again and can‘t remember how I did it.

First, you will need the id of the event. I have it in my $event array below:

$tickets = Tribe__Tickets__Tickets::get_all_event_tickets( $event['id'] )

This will give an array of ticket objects. The following function will show you the number of tickets available for the first ticket object. The events I am using for this project have two ticket types (adult and student), but they are sharing capacity, so I only need to know the number of tickets available for the first type to know the number available for the other. I think having shared capacity tickets is an Event Tickets Plus feature only.

$available = $tickets[0]->available()