Support

Account

Home Forums ACF PRO Force update of “Bidirectional”

Solved

Force update of “Bidirectional”

  • Hi.
    This is my Scenario:
    Custom Post Type “A”: has many ACF Fields, 1 of them is “Related to…”
    Custom Post Type “B”: has not so many ACF Fields, 1 of them is “This A are related to me”.
    I have just modified an ACF Field in “A” from Relationship to Relationship with Bidirectional that puts the relationship in the proper value of B.

    Now I need to open all “A” make a change in the relationship to enforce the update and add all the Relationship.

    “A” posts are >1000 so I’m looking if is possible do it in an automatic way …
    Just enforce an “update” in not enough…

    This code, doesn’t work.

    <?php
    include 'wp-load.php';
    print '<xmp>';
    $post_type   = 'MY_POST_TYPE A';
    $acf_field   = 'ACF_FIELD_OF_A_HAS_RELATIONSHIP';
    $batch_size  = 200;
    set_time_limit(0);
    
    $offset = 0;
    $total  = 0;
    
    do {
        $spots = get_posts([
            'post_type'      => $post_type,
            'post_status'    => 'any',
            'posts_per_page' => $batch_size,
            'offset'         => $offset,
            'fields'         => 'ids',
            'orderby'        => 'ID',
            'order'          => 'ASC',
            'no_found_rows'  => true,
        ]);
    
        foreach ($spots as $spot_id) {
            $value = get_field($acf_field, $spot_id);
    
            update_field($acf_field, $value, $spot_id);
    
            $total++;
            echo " • Post ID {$spot_id} \n";
        }
    
        $offset += $batch_size;
    } while (!empty($spots));
    
    echo "\n {$total} posts done.\n";
    

    Maybe to trigger bidirectional update there is something special to do?

  • SOLVED.

    
    <?php
    include 'wp-load.php';
    print '<xmp>';
    $post_type   = 'MY_POST_TYPE';
    $acf_field   = 'ACF_FIELD_HAS_RELATIONSHIP';
    $batch_size  = 200;
    set_time_limit(0);
    
    $offset = 0;
    $total  = 0;
    
    do {
        $spots = get_posts([
            'post_type'      => $post_type,
            'post_status'    => 'any',
            'posts_per_page' => $batch_size,
            'offset'         => $offset,
            'fields'         => 'ids',
            'orderby'        => 'ID',
            'order'          => 'ASC',
            'no_found_rows'  => true,
        ]);
    
        foreach ($spots as $spot_id) {
    
             // 1) Leggi il valore corrente (formato “raw” → third param FALSE)
             $value = get_field($acf_field, $spot_id, false);
    
             // Se è vuoto non serve forzare nulla
             if ($value === null || $value === '' || $value === []) {
                 echo " • Post {$spot_id}: empty, go on\n";
                 continue;
             }
    
             // 2) Svuota il campo TEMPORANEAMENTE
             delete_field($acf_field, $spot_id);      // rimuove sia meta che cache
             // oppure: update_field($acf_field, null, $spot_id);
    
             // 3) Riscrivi il valore originale ⇒ trigger completo degli hook
             update_field($acf_field, $value, $spot_id);
    
             $total++;
             echo " • Post {$spot_id}: update \n";
         }
        $offset += $batch_size;
    } while (!empty($spots));
    
    echo "\n {$total} posts done.\n";
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.