Skip to content

Snippet: Exclude Passed Events from Builder Elements

We recommend this newer snippet insted.
/**
 * Exclude Passed Events from Builder Elements.
 *
 * @link https://total.wpexplorer.com/docs/snippets/exclude-tribe-events-wpbakery/
 */
add_filter( 'vcex_query_args', function( $args ) {
    $showing_events = false;

    if ( isset( $query_args['post_type'] ) ) {
        if ( is_string( $query_args['post_type'] )
            && 'tribe_events' === $query_args['post_type']
        ) {
            $showing_events = true;
        } elseif ( is_array( $query_args['post_type'] )
            && 1 === count( $query_args['post_type'] )
            && in_array( 'tribe_events', $query_args['post_type'] )
        ) {
            $showing_events = true;
        }
    }

    if ( ! $showing_events ) {
        return $args;
    }

    $exclude_past_events = array(
        'key'     => '_EventStartDate',
        'value'   => date( 'Y-m-d' ) . ' 00:00:00',
        'compare' => '>=',
        'type'    => 'DATETIME'
    );

    if ( isset( $args[ 'meta_query' ] ) && is_array( $args[ 'meta_query' ] ) ) {
        $args[ 'meta_query' ][] = $exclude_past_events;
    } else {
        $args[ 'meta_query' ] = array( $exclude_past_events );
    }

    return $args;

} );
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top