Sorting dynamic select/dropdown for Contact Form 7 of Modern Tribe Events posts

For any poor soul out there that stumbles on the same, niche problem, the answer was frustratingly simple. Instead of trying to reinvent the wheel and build my own query, I should’ve been using the tribe_get_events function that I’m sure the Modern Tribe devs already worked hard to write.

Anyway, here’s the final solution for 1) adding a custom select box to Contact Form 7, and 2) populating it with upcoming events from The Events Calendar by Modern Tribe:

// Create post dropdown function for Contact Form 7 (not required)

add_action( 'wpcf7_init', 'postselect2' );
    function postselect2() {
        wpcf7_add_form_tag( 'postlist', 'custom_post_select2', true );
    }
    function custom_post_select2( $tag ) {
    $events = tribe_get_events( array(
        'posts_per_page' => -1,
        'start_date' => date( 'Y-m-d H:i:s' )
    ) );

    $output = "<select name="" . $tag["name'] . "' id='" . $tag['name'] . "' onchange="document.getElementById(\"" . $tag["name'] . "\").value=this.value;'> <option></option>";

    foreach ( $events as $event ) {
        $output .= '<option value="' . $event->EventStartDate . '&nbsp;' . $event->post_title . '">' . tribe_get_start_date( $event, true, 'n/j/Y &#64; g:ia' ) . '&nbsp;' . $event->post_title . '</option>';
    } // close foreach
    $output .= "</select>";
    return $output;
} // close function