• Resolved digitalbeans

    (@robbidb)


    Hi there,

    Bidirectional fields are working great when:

    • Adding a User to the Company Post Type
    • Adding a Company to the User

    This doesn’t update when importing Users with a Company ID attached.

    • When Users are imported, the Company is correctly added
    • The User is not shown though on the Company Post Type’s Users field

    I have tried adding the following to functions (the first can be ignored)

    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'         => 'r4l_companies',
            '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('field_6185189802029', $post_id, false);
            
            // Re-update the same value to force the update on the other side
            update_field('field_6185189802029', $my_field, $post_id);
            
        }
        
    }

    and:

    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 users
        $get_users = get_users(array(
            'number'            => '-1',
        ));
        
        // Bail early if not found
        if(empty($get_users)){
            return;
        }
        
        // Force bidirectional update
        add_filter('acfe/bidirectional/force_update', '__return_true');
        
        // Loop
        foreach($get_users as $user_id){
            
            // Retrieve unformatted value
            $my_field = get_field('field_6185189802029', 'user_'. $user_id, false);
            
            // Re-update the same value to force the update on the other side
            update_field('field_6185189802029', $my_field, $user_id);
            
        }
        
    }

    (I have used both the field names and keys).

    Would it be possible to help with this? ??

    Thanks very much for your help,
    Robbi

    • This topic was modified 3 years, 1 month ago by digitalbeans.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter digitalbeans

    (@robbidb)

    Please ignore this, I got there in the end :0)

    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 users
        $get_users = get_users(array(
            'number'            => '-1',
        ));
        
        // Bail early if not found
        if(empty($get_users)){
            return;
        }
        
        // Force bidirectional update
        add_filter('acfe/bidirectional/force_update', '__return_true');
        
        // Loop
        foreach($get_users as $user_id){
            
            // Retrieve unformatted value
            $my_field = get_field('field_6185189802029', $user_id, false);
            
            // Re-update the same value to force the update on the other side
            update_field('field_6185189802029', $my_field, $user_id);
            
        }
        
    }

    I will set up a Cron job for this to run every so often.

    Thanks for the great plugin!

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    I’m glad to hear that you managed to make it work!

    i’m not sure how you import those users, but yes, you’ll have to run that script during the import in order to update the companies.

    Have a nice day!

    Regards.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Bidirectional fields: CPT and Users – update on User import’ is closed to new replies.