Support

Account

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

Solving

Bidirectional relationships and WP All Import

  • I’m wondering if anyone here has experience importing data to bidirectional relationship fields using WP All Import.

    I have three CPTs that are all interconnected: Locations, Providers, and Services. Each CPT has bidirectional relationship fields for the other two.

    I know I have the target fields mapped correctly, because when I manually edit any of these posts, the relationships get updated in the reciprocal posts, i.e. if I edit John Doe (provider) and assign him to New York (location), if I then edit New York (location), I will see John Doe as one of its providers.

    I’m importing data into these CPTs using WP All Import with the ACF add-on. The import works, and the relationships are assigned in one direction, but the reciprocal relationships are not created during the import. I thought manually editing the posts might sync the relationship, but it doesn’t, unless I manually delete the relationship, save, and then manually re-enter it.

    I assume there’s a hook that fires off on a manual edit that isn’t firing when WP All Import runs. I realize it just may not be possible to get WP All Import to do this, so I’m considering writing my own small utility that will blast through all of the posts and write the missing relationships. If anyone has done something like that, I’d appreciate a code sample to save some time in working out what needs to be done. Thanks.

  • I guess maybe I’m the only person dealing with this issue, but for what it’s worth, I’ve found a solution.

    I built a special utility to crank through all of my CPTs with these bidirectional relationships and sync them up. It’s based heavily on the ACF acf_update_bidirectional_values() function, but I had to make my own modified version of the function to deal with one specific issue.

    The function assumes that all of the current values already have the proper reciprocal relationship in place, and only handles any changes happening on the current edit. But in my case, only half of the bidirectional relationship exists.

    My version just modifies the definition of $current_values (line 56 in the file acf-bidirectional-functions.php, as of version 6.8.0.1) to be an empty array.

    This change works to make sure the missing values get added, but I should note that it prevents subtractions from working. Still, I think the ACF function could possibly stand to be reworked… right now it assumes that all of the existing values already have the reciprocal relationship.

    When things are working properly, that should always be the case. But as I’ve shown here, there are situations where that is not a safe assumption. (I don’t think it’s _that_ much of an edge case that I am importing data with WP All Import, especially since it has a dedicated ACF add-on.)

  • 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);
          }
        }
      }
    }
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.