Support

Account

Home Forums Add-ons Repeater Field update_field only works if followed by exit;

Solved

update_field only works if followed by exit;

  • Hi,

    Strange one here that’s probably something simple but I’ve been looking at it too long, hopefully fresh eyes can spot it.

    I have a repeater field that contains a date and a text field, and I’m trying to add a new row when the status of the post changes:

    function check_custom_status_change( $new, $old, $post ) {
            $actions = get_field( 'history', $post->ID);
            $actions[] = array(
                'date' => date('F j, Y g:i a'),
                'action' => 'Status changed '.$old.' to '.$new
            );
            update_field( 'history', $actions, $post->ID );
    }
    add_action( 'transition_post_status', 'check_custom_status_change', 20, 3 );

    The above code fails to add a new row .. however simply exiting the code after the update_field works as expected:

    function check_custom_status_change( $new, $old, $post ) {
            $actions = get_field( 'history', $post->ID);
            $actions[] = array(
                'date' => date('F j, Y g:i a'),
                'action' => 'Status changed '.$old.' to '.$new
            );
            update_field( 'history', $actions, $post->ID );
            exit;
    }
    add_action( 'transition_post_status', 'check_custom_status_change', 20, 3 );

    Am I missing something obvious here? Thanks in advance.

  • That’s odd. If you don’t do the exit and you reload the page again after the save does the repeater show the newly added row?

  • No, the new row doesn’t show and there’s is no new entry in the wp_postmeta table for that post_id. I’ve even tried to simplify the code by using add_row and the effect is the same:

    $row = array(
                'date' => date('F j, Y g:i a'),
                'action' => 'Status changed '.$old.' to '.$new
            );
            $new_row_count = add_row( 'history', $row, $post->ID );

    The above fails to add the new row (and no new entry in the database) whereas:

    $row = array(
                'date' => date('F j, Y g:i a'),
                'action' => 'Status changed '.$old.' to '.$new
            );
            $new_row_count = add_row( 'history', $row, $post->ID );
            var_dump( $new_row_count );
            exit;

    does add the new entry as expected (and is added ok to the wp_postmeta table) and the $new_row_count is as expected.

    I’ll continue to debug by stepping into the ACF core and adding die() points to see if I can pin down exactly where it’s going wrong.

    *Update* I think this is all down to me trying to add a new repeater row when the post status changes and WordPress does that in an Ajax request. I think the simplest solution is just to maintain a separate ACF ‘Status’ field which will also have the benefit of allowing me more statuses 😉

  • I was curious about it being during an ajax action. I would say that some of the actions in WP are not being fired.

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

You must be logged in to reply to this topic.