Support

Account

Home Forums Add-ons Repeater Field Bidirectional with repeater issue Reply To: Bidirectional with repeater issue

  • I have never seen the article that you linked, but I find it very interesting. I had not thought of the work-a-round suggested making bidirectional relationships work in repeaters. However, I do think it would be a little confusing for some.

    The idea is to, for example, you my plugin to create a bidirectional relationship field at the top level. For example a field named “related_events_people”. This relationship is added at the top level of both the events CPT and the people CTP. This filed will hole all of the relationships between these 2 CPTs. The main purpose of a bidirectional relationship field is to easily find the related posts from either site and this would accomplish this. However, in this case this field will not be editable and it’s value will only be updated dynamically.

    then the idea is that when an events post is saved you loop over the repeater and get the values from the relationship field in the repeater and you push the merged values from the repeater into the top level relationship field. Updating an ACF relationship field automatically triggers my plugin to update the top level relationship field on the the related posts.

    
    add_action('acf/save_post', 'repeater_bidirectional_update', 20);
    function repeater_bidirectional_update($post_id) {
      $update_field = 'field_XXXXXXX'; // field key of top level relationship field
      $collected_posts = array();
      if (have_rows('repeater_field_name', $post_id)) {
        while (have_rows('repeater_field_name', $post_id)) {
          the_row();
          // get the value of the relationship sub field
          // 2rd argument is false, do not format value
          // causes only post IDs to be returned
          $values = get_sub_field('relationship_field', false);
          if (!$empty($values)) {
            if (!is_array($value)) {
              // this check insures the value is an array
              // post object fields return a single value
              $values = array($values);
            }
            // loop over related posts and add them to collected posts
            foreach ($values as $value) {
              if (!in_array(intval($value), $collected_posts)) {
                // add if not added before
                $collected_posts[] = intval($value);
              } // end if
            } // end foreach value
          } // end if !empty
        } // end while have_rows
      } // end if have_rows
      
      // update the top level relationship field
      // with collected posts
      update_field($update_field, $collected_posts, $post_id);
      
    } // end function
    

    You’re obviously going to need to do more because you have a nested repeater, so you’ll need a nested loop and you’ll need to collect posts from 2 separate repeagter sub fields and update 2 top level relationship fields.

    The last step I would take, after I am sure everything is working would be to make the top level field not editable by removing it.

    
    // again, field key of top level field
    add_filter('acf/prepare_field/name=related_events_people', 'my_remove_field');
    function my_remove_field($field) {
      return false;
    }