• Resolved tnightingale

    (@tnightingale)


    I have a PODS form that displays a relationship field (relating the currently logged in user to a custom post type). The custom posts are listed in the form as checkboxes with the post titles. The form works well, but I need it to do 3 things differently:

    1) Filter which posts are displayed in the form, based on an ACF date field (don’t want to display past events).

    2) sort them by that field, ascending by date

    3) display the date field as well as the post title, on the checkbox item (not as important as the other 2 but nice to have)

    I was trying to find a list of the available arguments for the fields included in the form, but nothing came up on your documentation site. I know it’s under construction. The examples for forms include only ‘label’ and ‘default’.

    Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Paul Clark

    (@pdclark)

    <?php
    /**
     * Plugin Name: Pods — Filter Relationship Field
     * Description: Add <code>event_date</code> ACF field to <code>posts_by_author</code> frontend form field. Sorts by date. Hides past events.
     * Version: 1
     * Author: ??δ???
     * Author URI: https://pd.cm
     */
    add_filter(
      'pods_field_dfv_data',
      function( $data, $args, $attributes ) {
    
        // Meta keys for relationship field and event field.
        $field_name = 'posts_by_this_author';
        $date_field_meta_key = 'event_date';
    
        // Don't run unless it's the associated relationship field.
        if ( 'pods_field_' . $field_name !== $data['fieldConfig']['name'] ) {
          return $data;
        }
    
        // For each related post as $item.
        foreach( $data['fieldItemData'] as $key => & $item ) {
          $event_date = strtotime( get_post_meta( $item['id'], $date_field_meta_key, true ) );
    
          // Don't display past events.
          if ( $event_date < time() ) {
            unset( $data['fieldItemData'][ $key ] );
          }
    
          // Display the date field as well as the post title.
          $item['name'] = sprintf(
            '%s: %s',
            date(
              'Y-m-d', // https://www.php.net/manual/en/datetime.format.php
              $event_date
            ),
            wp_strip_all_tags( $item['name'] )
          );
    
          // Store date.
          $item[ $date_field_meta_key ] = $event_date;
        }
    
        // Sort by date.
        usort(
          $data['fieldItemData'],
          function( $a, $b ) use ( $date_field_meta_key ) {
            if ( $a[ $date_field_meta_key ] === $b[ $date_field_meta_key ] ) { return 0; }
            return $a[ $date_field_meta_key ] < $b[ $date_field_meta_key ] ? -1 : 1;
          }
        );
    
        return $data;
      },
      10,
      3
    );
    Thread Starter tnightingale

    (@tnightingale)

    Thanks so much – works perfectly!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to filter the displayed form fields?’ is closed to new replies.