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