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.

  • Order
  • Label
  • Name
  • Key
  • Type

Compatible Fields

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

The system also works with sub fields of Groups & Clones (prefixed field names must be turned off). However, note that it won’t work in a Repeater or Flexible Content.

ACFRelationship
ACFPost Object
ACFUser
ACFTaxonomy

Possible Configurations

Post <> Post

┌ Display a Post Object field on "Post Type A" showing "Post Type B"
└ Display a Post Object field on "Post Type B" showing "Post Type A"

Taxonomy <> Taxonomy

┌ Display a Taxonomy field on "Taxonomy A" showing "Taxonomy B"
└ Display a Taxonomy field on "Taxonomy B" showing "Taxonomy A"

Taxonomy <> User

┌ Display a Taxonomy field on "Users" showing "Taxonomy A"
└ Display a User field on "Taxonomy A" showing "Users"

Taxonomy <> Post

┌ Display a Taxonomy field on "Post Type A" showing "Taxonomy A"
└ Display a Post Object field on "Taxonomy A" showing "Post Type A"

User <> Post

┌ Display a User field on "Post Type A" showing "Users"
└ Display a Post Object field on "Users" showing "Post Type A"

User <> User

┌ Display a User field on "Users" showing "Editor"
└ Display a User field on "Users" showing "Admin"

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);
        
    }
    
}