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).

Compatible Fields

The bidirectional setting is compatible with 4 fields that can be linked together.

  • Relationship
  • Post object
  • User
  • Taxonomy terms

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 terms of performance. Which means that it checks if the old value is different of the new value, to avoid saving and saving again the same existing value (on both sides).

If your website already have existing field values, you’ll have to use the following hook to force the update, despiste the fact that the old value is the same as new one. Once added you’ll have to save each posts individually:

add_filter('acfe/bidirectional/force_update/name=my_field', '__return_true');

Additionally, you can create a script in order to populate those values automatically. Code example:

add_action('admin_init', 'my_acf_update_bidirectional_posts');
function my_acf_update_bidirectional_posts(){
    
    // Retrieve all pages
    $get_posts = get_posts(array(
        'post_type'         => 'page',
        'posts_per_page'    => -1,
        'fields'            => 'ids',
    ));
    
    // Bail early if no post was found
    if(empty($get_posts))
        return;
    
    // Force bidirectional update
    add_filter('acfe/bidirectional/force_update/name=my_field', '__return_true');
    
    foreach($get_posts as $post_id){
        
        // Retrieve Data
        $my_field = get_field('my_field', $post_id, false);
        
        // Update Data
        update_field('my_field', $my_field, $post_id);
        
    }
    
    add_filter('acfe/bidirectional/force_update/name=my_field', '__return_false');
    
}