Dropdown criteria / custom field > current date > future dates only

Hello there fellow gravity users. This is proving a little difficult for me I have tried all manner of variations using wp_query and simply cannot get it to work. I therefore if possible would like some suggestions thanks.

The code below works correctly and populates a dropdown showing all posts from a certain category.

My issue arises when I try/wish to get a little more complex. I have a class_date stored in custom field class_date

I wish to query and display only posts that are

  • category 9 (Class-Date)
  • class_date - is today
  • class_date - is in the future

Essentially I do not wish to have the ability to see past dates. This stops a user selecting past classes.

Your knowledge and wisdom would be gratefully appreciated.:+1:

Regards

Adey


Code below

// POPULATE DROPDOWN WITH POSTS

add_filter( 'gform_pre_render_18', 'populate_posts' );
add_filter( 'gform_pre_validation_18', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_18', 'populate_posts' );
add_filter( 'gform_admin_pre_render_18', 'populate_posts' );
function populate_posts( $form ) {
 
    foreach ( $form['fields'] as &$field ) {
 
        if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
            continue;
        }

		
		// Get posts that contains
		// category id 1
		// post status published
		// order by title.
		
        $posts = get_posts( 'numberposts=-1&category=9&post_status=publish&order_by=title' );
 
        $choices = array();
 
        foreach ( $posts as $post ) {
            $choices[] = array( 'text' => $post->the_title, 'value' => $post->the_title );
        }

This is more of a WordPress question than a Gravity Forms question. You may get more attention at Stack Overflow?

Ok thanks Chris.

I was hoping someone had done similar. You have been around here for years I will take your word for it. Shame but such is life.

1 Like

I’ll leave this open in case some WP_Query guru has experience with that.

Thanks Chris much appreciated. Plus helpful for others if someone does present a solution.:+1: