Filter Entries by Time Field

🧪

Experimental Snippet

This snippet was discovered in the wizard’s chaotic laboratory — an unpolished experiment, conjured to solve a niche challenge. Use at your own risk.

Filter entry choices by a Time field on the source form. For example, if your source form is used to collect events, including their start date and time, use this snippet to only populate events that have yet to start in your target form.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gppa-filter-entries-by-time-field.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
/**
 * Gravity Perks // Populate Anything // Filter Entries by Time Field
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * Experimental Snippet 🧪
 *
 * Filter entry choices by a Time field on the source form. For example, if your source form is used
 * to collect events, including their start date and time, use this snippet to only populate events
 * that have yet to start in your target form.
 */
// Update "123" to your form ID and "4" to the field being populated with choices.
add_filter( 'gppa_input_choices_123_4', function ( $choices, $field, $objects ) {

	// Update "5" to the Time field ID on your source form.
	$time_field_id = 5;

	$new_choices = array();

	foreach ( $choices as $choice ) {
		// Convert GF Time field time (e.g. 12:00 pm) to timestamp.
		$choice_time = new DateTime( $choice['object']->$time_field_id, wp_timezone() );
		if ( $choice_time > current_datetime() ) {
			$new_choices[] = $choice;
		}
	}

	if ( empty( $new_choices ) ) {
		$new_choices[] = array(
			'text'       => 'No items available.',
			'value'      => '',
			'isSelected' => false,
		);
	}

	return $new_choices;
}, 10, 3 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.