Home Forums General Issues How can i update my subfields


How can i update my subfields

  • Hello. I try to update my subfields but nothing hapens. This code print field value but it cant update the field.
    fields scheme

    function some_field_update(){
        if( have_rows('main_specifications', 20735) ):
            while ( have_rows('main_specifications', 20735) ) : the_row();
                echo get_sub_field('colors') . '<br>';
                if (update_sub_field('colors', 'some value', 20735)){
                    echo 'successful';
                    echo 'false';
        else :
            echo 'no rows found';
    add_action('init', 'some_field_update');

    i pasted it into functions.php. ACF Version is 5.8.12.

  • have you tried

    update_sub_field('colors', 'some value') // no post id needed here when in loop

    other than this I don’t see anything wrong with the code you supplied. If this does not fix the issue then you’ll need to explain better what you are trying to accomplish.

  • Thanks, yes i tried this approach and it doesn’t work for me. I updated these fields with update_post_meta but now i can see these fields values only on post edit page. And function get_fields cant get their values. If i click update post then function works correctly. But i cant update all the posts using this approach, there are about 8000 posts.
    I used this code to update the fields

    function some_field_update(){
        $my_posts = new WP_Query;
        $myposts = $my_posts->query( array(
            'post_type' => 'product',
            'posts_per_page' => 10000,
        ) );
        foreach( $myposts as $pst ){
            if ($pst){
                update_post_meta($pst->ID, 'main_specifications_colors', get_post_meta($pst->ID, 'colors')[0]);
    add_action('init', 'some_field_update');

    And this code i use to display the values

    $fields = get_fields($product_id);
        <?php if( $fields ): ?>
            <?php foreach( $fields as $name => $value ):
                $field = get_field($name, $product_id);
                if (array_filter($field)){
                    echo '<h2>'.ucwords(str_replace('_', ' ', $name)).':</h2>';
                    echo '<table>';
                    foreach ($field as $key => $val){
                            echo '<tr><td>'. ucwords(str_replace('_', ' ', $key)).': </td>';
                            echo '<td>'. ucwords(str_replace('_', ' ', $val)).'</td></tr>';
                    echo '</table>';
            <?php endforeach; ?>
        <?php endif;
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘How can i update my subfields’ is closed to new replies.