0

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: enter image description here

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 :)

  • If anybody happens across this, I'm still stuck at the moment. At this point, the code I have listed in my question is still the closest to functional that I've been able to compile and I really don't know what else to try to get things to show in the correct order! – iprobablyhavenoanswers Sep 24 '18 at 17:06
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.