Support

Account

Home Forums Backend Issues (wp-admin) Bidirectional relationships and WP All Import Reply To: Bidirectional relationships and WP All Import

  • In case anyone here wants to make their own utility for this purpose, mine relies mainly on two things: a custom version of the acf_update_bidirectional_values() function that sets $current_values to an empty array (which I call my_acf_update_bidirectional_values(), and then the following custom function.

    This function accepts two inputs. $posts is an array of the posts you want to iterate over (the ones containing the correct values), retrieved via get_posts(); $fields is a simple array of the field keys or names that hold the bidirectional relationships.

    function my_acf_bidi_sync($posts=[], $fields=[]) {
      foreach ((array)$posts as $post) {
        foreach ((array)$fields as $fkey) {
          $field = get_field_object($fkey, $post->ID);
          if (!empty($field['value'])) {
            $mapped_ids = array_map(function($v) { return $v->ID; }, $field['value']);
            my_acf_update_bidirectional_values($mapped_ids, $post->ID, $field);
          }
        }
      }
    }