Use a date field in Gravity Forms to set the value of a Types date field

Gravity Forms can be used to generate new posts. You can add custom fields to the form and capture custom field data for that new post. However, the date field format used by Gravity Forms is incompatible with the date field format required by Types. To fix this, you must use some custom code to convert the Gravity Forms date into a format Types can understand before saving it in the Types date field. Add this code snippet to your child theme’s functions.php file, or create a new snippet in Toolset > Settings > Custom Code.

/* --------------------------------------------------------------------------------------- */
// CONVERT GRAVITY FORMS DATE FIELD TO TYPES FIELD FORMAT
// This function hooks in before data from a GF form is saved to create a new post
// GF date format is not Unix timestamp, so this code converts a date to the proper format
// ----------------------------------------------------------------------------------------*/
add_filter("gform_post_data", "convert_gf_date_to_types_date", 10, 3);
function convert_gf_date_to_types_date( $post_data, $form, $entry ) {
  $gf_form_id       = 123; // the gravity form numeric id
  $gf_field_id      = 456; // the date field numeric id
  $types_field_slug = 'types-field-slug'; // the types field slug (without the wpcf- prefix)

  // you should not edit below this line

  //check if this is the right form
  if ( isset($form['id']) && $form['id'] == $gf_form_id ) {
  // add unix timestamp for date field in meta_input
  $post_data['meta_input']['wpcf-' . $types_field_slug] = strtotime(rgar($entry, $gf_field_id));
  }
  return $post_data;
}

Change 123 to match the Gravity Form numeric ID. Change 456 to match the Gravity Forms date field numeric ID. You can find that by hovering over the field in the Gravity Forms form editor. Change types-field-slug to match the slug of the Types custom date field. Do not use the ‘wpcf-‘ prefix here.

Filter a View to show Events that occur within a date range using start and end date filters

Let’s say you have a CPT of Events. Each event has a start date and an end date. You want to set up a View that will find Events that happen between two dates, but that doesn’t necessarily mean the start and end dates of the Events will fall between the start and end dates selected by your filters. So you need to use some date logic to determine if the event overlaps with the date range your User selects in the filters.

By default, this View will only show events beginning this current month. Any date filters selected by the User will override these defaults.

add_filter( 'wpv_filter_query', 'default_this_month_inclusive', 10, 3 );
function default_this_month_inclusive ( $query, $view_settings, $view_id ) {
  if( $view_id == 12345 ) {
    $selected_start = strtotime('0:00:00');
    $selected_end = 9999999999;

    // determine if user has selected a start or end date, and destroy those meta query items
    if ( isset($query['meta_query']) ) {
      foreach($query['meta_query'] as $key => $meta) {
        if(isset($meta['key']) && $meta['key'] == 'wpcf-event-start-date'){
          // get a timestamp that represents 12:00 am on the event start date
          $selected_start = strtotime(date('m/d/Y', $meta['value']) . ' 0:00:00');
          unset($query['meta_query'][$key]);
        }
        if(isset($meta['key']) && $meta['key'] == 'wpcf-event-end-date'){
          // get a timestamp that represents 11:59:59 pm on the event end date
          $selected_end = strtotime(date('m/d/Y', $meta['value']) . ' 23:59:59');
          unset($query['meta_query'][$key]);
        }
      }
    }

    $args = array(
      'relation' => 'AND',
      array(
        'key' => 'wpcf-event-start-date',
        'value' => $selected_end,
        'compare' => '<=',
        'type' => 'numeric'
      ),
      array(
        'key' => 'wpcf-event-end-date',
        'value' => $selected_start,
        'compare' => '>=',
        'type' => 'numeric'
      )
    );
    // add these arguments to your meta query
    $query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
    $query['meta_query'][] = $args;
  }
  return $query;
}

Update your View ID and custom field slugs if necessary.