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);
}
}
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.