Get posts with tickets

Home Forums Ticket Products Event Tickets Plus Get posts with tickets

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1271126
    Johannes
    Participant

    Hi there,

    What is the best way to retrieve the posts that have tickets for sale?
    I only have the Event Tickets and Event Tickets Plus plugin.

    I found this way to get the posts with tickets in it. However, it still lists the events that are sold out or have tickets that are not on sale anymore.

                          $cache = tribe( 'tickets.cache' );
                            $cache->include_past( false );
                            $posts = $cache->posts_with_ticket_types( array( 'twt_event' ), true);

    Thank you!

    #1271346
    Barry
    Member

    Hi Johannes,

    One approach would be to take your existing result set then iterate through all of the tickets and discard those that are not current or are out of stock. You could then cache the result.

    It would probably also be possible to craft one or more queries to get at the same data; which works and performs best probably depends on the number of events and tickets you are dealing with.

    Let me know if you need further help!

    #1272600
    Johannes
    Participant

    Thank you!
    I ended up using something like this:

    function get_events_with_registration() {
    	$cache = tribe( 'tickets.cache' );
    	$cache->include_past( false );
    	$event_ids = $cache->posts_with_ticket_types( array( 'twt_event' ), true);
    	if(empty($event_ids)) {
    		return false;
    	}
    	$events = array();
    	foreach($event_ids as $event_id) {
    		$event = get_post($event_id);
    		if(tribe_events_has_soldout($event)) {
    			continue;
    		}
    
            //check if in date range
    		$tickets_available = false;
    		foreach ( Tribe__Tickets__Tickets::get_all_event_tickets( $event ) as $ticket ) {
    			if(tribe_events_ticket_is_on_sale($ticket)) {
    				$tickets_available = true;
    				break;
    			}
    		}
    
    		if($tickets_available) {
    			$events[] = $event;
    		}
    	}
    	if(empty($events)) {
    	    return false;
    	}
    	return $events;
    }
    #1272932
    Cliff
    Member

    Johannes, great job and thanks so much for sharing!

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Get posts with tickets’ is closed to new replies.