Support

Account

Home Forums General Issues Querying custom field with bidirectional relationship Reply To: Querying custom field with bidirectional relationship

  • Yeah sorry, I’m new to the plugin. I’ll try to explain it better.

    So, the developer before me implemented Gravity Forms and ACF in the website that I’m currently working on. The data of these gravity forms are stored in wp_postmeta. 2 of those fields are text data fields, 2 other fields contain a date (a begin date and an end date).
    Now, there is a custom field that was made using ACF (called linked_requests). This field’s value can currently only be set via editing a post manually (using the bidirectional relationship function of ACF). The custom field contains other post id’s that match based on a couple criteria. The criteria is based on the 2 text data fields having to containing the exact same value, and the begin date and end date have to be within the same timeframe as well when iterating through the posts.

    My current approach is iterating through the posts, then use get_post_meta to get the meta_value (based on the meta_key) that’s linked to the post_id:

    
    add_action( 'gform_after_submission_1', 'link_requests_action_after_apc', 10, 2 );
    function link_requests_action_after_apc( $entry, $form ) {
      $requests = new WP_Query([
        'post_type' => ['request'],
        'orderby' => 'ID',
        'order' => 'ASC',
        'post_author' => 122,
      ]);
      
      if ($requests->have_posts()) :
    
        while ($requests->have_posts()) : $requests->the_post();
    
          $preferred_driver_locations = get_post_meta('preferred_driver_locations');
    
          $driver_type = get_post_meta('driver_type');
          
          $authority = '';
    
          $from_date = get_post_meta('from_date');
          $until_date = get_post_meta('until_date');
    
        endwhile;
    
      endif;
    }
    

    Then I have to iterate through the custom field (linked_requests), to see if this is empty, contains a serialized array or if the custom field does not exist yet (for that particular post_id). If the field contains a serialized array, then I have to read it and check if the array contains the current user’s post_id. If not, then I have to add the post_id to the serialized array (so unserialize and serialize when updating the field).

    How would I got about doing this? And how can I maintain that bidirectional relationship when it comes to that custom field?