tribe_tickets_get_capacity( int|WP_Post $post )

Returns the capacity for a given Ticket/RSVP.

Note while we can send a post/event we only store capacity on tickets/rsvps so when provided an event it will hand off to tribe_get_event_capacity().


Parameters

$post

(int|WP_Post) (Required) Post (ticket!) we are trying to fetch capacity for.


Top ↑

Return

(int|null)


Top ↑

Source

File: src/template-tags/tickets.php

function tribe_tickets_get_capacity( $post ) {
	// When not dealing with a Instance of Post try to set it up
	if ( ! $post instanceof WP_Post ) {
		$post = get_post( $post );
	}

	// Bail when it's not a post or ID is 0
	if ( ! $post instanceof WP_Post || 0 === $post->ID ) {
		return null;
	}

	$event_types = Tribe__Tickets__Main::instance()->post_types();
	$key = tribe( 'tickets.handler' )->key_capacity;

	// When we have a legacy ticket we migrate it
	if ( ! in_array( $post->post_type, $event_types ) && tribe( 'tickets.version' )->is_legacy( $post->ID ) ) {
		$legacy_capacity = tribe( 'tickets.handler' )->filter_capacity_support( null, $post->ID, $key );

		// Cast as integer as it might be returned as numeric string on some cases
		return (int) $legacy_capacity;
	}

	// Defaults to the ticket ID
	$post_id = $post->ID;

	// Return Null for when we don't have the Capacity Data
	if ( ! metadata_exists( 'post', $post->ID, $key ) ) {
		$mode = get_post_meta( $post->ID, Tribe__Tickets__Global_Stock::TICKET_STOCK_MODE, true );
		$shared_modes = array( Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE, Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE );

		// When we are in a Ticket Post Type update where we get the value from Event
		if (
			! in_array( $post->post_type, $event_types )
			&& in_array( $mode, $shared_modes )
		) {
			$event_id = tribe_tickets_get_event_ids( $post->ID );

			// It will return an array of Events
			if ( ! empty( $event_id ) ) {
				$post_id = current( $event_id );
			}
		} else {
			return null;
		}
	}

	// Fetch the value
	$value = get_post_meta( $post_id, $key, true );

	// When dealing with an empty string we assume it's unlimited
	if ( '' === $value ) {
		$value = -1;
	}

	return (int) $value;
}

Top ↑

Changelog

Changelog
Version Description
4.6 Introduced.