Self/Multi/Bidirectional Fields

An advanced bidirectional setting (also called post-to-post) that link fields values between each others. Fields will work bidirectionally and automatically update each others. Works in Groups & Clones (prefixed field names must be turned off).

  • Order
  • Label
  • Name
  • Key
  • Type

Compatible Fields

The bidirectional setting is compatible with four ACF Fields that can be linked together.

ACFRelationship
ACFPost Object
ACFUser
ACFTaxonomy

Usage Example

  1. Create a field group “Page: Relation” displayed on the post type: page
  2. Inside it, create a relationship field, allowing the post type: post
  3. Create a field group “Post: Relation” displayed on the post type: post
  4. Inside it, create a relationship field, allowing the post type: page
  5. Activate the “Bidirectional” setting and select “Page: Relation” relationship field
  6. Edit any page, and select any post in the relationship field
  7. The page is now also saved in the said post relationship field

Existing Values

The Bidirectional Setting has been designed to be optimized in term of performance. Which means that it checks if the old value is different of the new value, to avoid to unnecessarily save again and again the same existing value on both sides.

If your website already have existing values inside a Post Object or Relationship Field before you enabled the Bidirectional Setting, then you’ll have to force values update on both sides.

Simply add the following code in your theme’s functions.php file, and save each post individually in the WordPress Admin. Once all posts are updated, you can remove it:

// always update values of all bidirectional fields
add_filter('acfe/bidirectional/force_update', '__return_true');

// or target a specific field only
add_filter('acfe/bidirectional/force_update/name=my_field', '__return_true');

An another solution would be to create a script that will loop through all posts and force the update of existing values. This script should be run only once, and removed when all posts are correctly updated. Script example:

add_action('admin_init', 'my_acf_update_bidirectional_posts');
function my_acf_update_bidirectional_posts(){
    
    // bail early if ajax request
    if(wp_doing_ajax()){
        return;
    }
    
    // Retrieve all pages
    $get_posts = get_posts(array(
        'post_type'         => 'page',
        'posts_per_page'    => -1,
        'fields'            => 'ids',
    ));
    
    // Bail early if not found
    if(empty($get_posts)){
        return;
    }
    
    // Force bidirectional update
    add_filter('acfe/bidirectional/force_update', '__return_true');
    
    // Loop
    foreach($get_posts as $post_id){
        
        // Retrieve unformatted value
        $my_field = get_field('my_field', $post_id, false);
        
        // Re-update the same value to force the update on the other side
        update_field('my_field', $my_field, $post_id);
        
    }
    
}