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

What I have is a dynamic dropdown/select box in Contact Form 7 I believe to be based on this: https://gist.github.com/morgyface/56474f0a37abb7d622880daf6eff6e40

It's been a while since I worked on this so details are a little foggy, but we've only recently realized there's a problem with the order and I'm not really even sure where to start looking to fix it. I remember this whole thing taking me way too long to figure out to the point I have it now, and it's been long enough since I was digging in it that I'm having a hard time here!

Here's the modified code that, aside from the order, is working:

add_action( 'wpcf7_init', 'postselect2' );
function postselect2() {
    wpcf7_add_form_tag( 'postlist', 'custom_post_select2', true ); //If the form-tag has a name part, set this to true.
}
function custom_post_select2( $tag ) {
    $posttype = 'tribe_events';
    $date = new DateTime();
    $today = $date-getTimestamp();
    $args = array(
        'post_type'         = $posttype,
        'post_status'       =  'publish',
        'numberposts'       = -1,
        'meta_key'          = '_EventStartDate',
        'orderby'           = 'meta_value_num',
        'order'             = 'ASC',
        'meta_query'        = array(
            'relation'          = 'AND',
                array(
                    'key'           = '_EventStartDate',
                    'compare'       = '=',
                    'value'         = $today,
                )
        )
    );
    $posts = get_posts( $args );
    $output = "select name='" . $tag['name'] . "' id='" . $tag['name'] . "' onchange='document.getElementById(\"" . $tag['name'] . "\").value=this.value;'option/option";
    // if you've set the name part to true in wpcf7_add_form_tag use $tag['name'] instead of $posttype as the name of the select.
    foreach ( $posts as $post ) {
            $postid = $post-ID;
            $posttitle = get_the_title( $postid );
            $postslug = get_post_field( 'post_name', $postid );
            $postdate = tribe_get_start_date($post, false, $date_format='m/d/y');
        $output .= 'option value="' . $postdate . 'nbsp;' . $posttitle . '"' . $postdate . 'nbsp;' . $posttitle . '/option';
    } // close foreach
    $output .= "/select";
    return $output;
}

One other aspect here is, since there are events for which people are filling out the form to request registration, events occurring before "today" obviously shouldn't show up in the list (working).

Here's an example of the output:

The events show up correctly on the actual Events page with the [mostly stock] loop from Modern Tribe, so I'm confident it isn't an issue with the event posts themselves.

TIA for any tips or advice :)

Topic the-events-calendar dropdown select plugin-contact-form-7 php Wordpress

Category Web


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.