Support

Account

Home Forums Backend Issues (wp-admin) update_field() issues

Solved

update_field() issues

  • Using the latest ACF Pro. I’m trying to convert categories into post meta programmatically. I’ve got to the point where I’m running the script that adds the category values to the fields.

    However I’m having problems with that seem to be related to the fact that the posts do not already “have” the fields – i.e., they haven’t been created or updated since the field group was created.

    I’ve been trying this:

    if (false == update_sub_field(
        array('repeater_name', 1, 'sub_field_name'), 
        $value, 
        $postId
    )) {
        update_field(
           'repeater_name', 
            array(
                array('sub_field_name' => $value)
            ), 
            $postId
        );
    }

    But the result is wrong. When I run get_field('repeater_name'), I see an array in the format sub_field_key => '', where sub_field_key is the key of the (correct) sub-field.

    However if I save the post, without filling in the fields, the update script runs correctly and then get_field('repeater_name') returns an array in the format sub_field_name => $value

    I have tried running wp_update_post() beforehand but that doesn’t help.

    I’ve also tried using ACF’s add_row() function, but again I get errors and I presume it’s to do with the post not already “having” the field to add the row to.

    What is the correct way to add these values?

    P.S. I’ve been running this script on the admin_footer hook.

  • Update: Actually, I was wrong about the result of get_field() after running the script. What I reported was actually the result of get_field() only after running the update_sub_field() function.

    However what is actually happening is weird. In the database, the result of running the script on un-saved posts is that the wp_postmeta table entries have some issues.

    Here’s what the correct database entries look like (meta_key and meta_value entries):

    _repeater_name..........................repeater_field_key
    repeater_name............................1
    _repeater_name_0_sub_field_name..........sub_field_key
    repeater_name_0_sub_field_name...........$value

    But here’s what I get:

    _repeater_name..........................
    repeater_name............................a:1:{i:0;a:1:{s:16:"sub_field_name...
    _repeater_name_0_sub_field_name..........
    repeater_name_0_sub_field_name...........$value

    So instead of the row count, the repeater_name entry has a serialized array of the sub fields and all the field keys are missing.

  • Hi squarestarmedia,

    As you have observed, ACF needs to have a value present on a Post Object for field name<->field key lookups to function correctly.

    The solution would be to use the repeater field’s key, (e.g. ‘field_abcdef123456’) instead of the name (e.g. ‘repeater_name’) when referencing the repeater.

    If I understand your use case correctly, you might also want to purge all rows in the repeater, and then add the taxonomies, so as not to have stale taxonomies should some be removed (either as non-hierarchical, or as a change in structure for hierarchical taxonomies).

    Example:

    
    <?php
    // For every row in the repeater
    while ( have_rows( 'field_abcdef123456' ) ) {
        // Delete the first row, until it is empty
        delete_row('field_abcdef123456', 1);
    }
    
    // For each taxonomomy...
    add_row( 'field_abcdef123456', array(
        'sub-field-name-or-key' => 'sub-field-value',
        // ...
    ) );
    

    ——————

    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

  • Thanks for the input Jonathon. While the code example you provided caused some issues (including an infinite loop when I added the post_id to the have_rows() function!), the field key suggestion was instrumental in solving it. What does work is this:

    if (false == update_sub_field(
        array('repeater_key', 1, 'sub_field_name'), 
        $value, 
        $postId
    )) {
        update_field(
           'repeater_key', 
            array(
                array('sub_field_name' => $value)
            ), 
            $postId
        );
    }

    The difference is using the repeater’s key instead of name in the update functions.

  • Sorry about the issues! Glad you got it fixed though 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘update_field() issues’ is closed to new replies.