Support

Account

Home Forums ACF PRO update field is updating value in database but on the frontend

Solved

update field is updating value in database but on the frontend

  • If I run the following code it inserts the value in the database but on the front end the value is blank.
    The field is:
    field type – post object
    filter by post type – team
    return format – post object

    function items_taken($entry, $form){
    $team_code = rgar( $entry, '10' );
    $field_id = 8; 
    $field = RGFormsModel::get_field( $form, $field_id );
    $post_id = is_object( $field ) ? $field->get_value_export( $entry ) : '';
    update_field( 'field_59f6d4dd91fcd', $team_code, $post_id );
    }

    The field is field_59f6d4dd91fcd is showing the correct value ‘$team_code’ in the database but when I go into the post the value on the front end is blank.
    Why is this?

  • what is the value that is being saved ($team_code), is this a post ID?

  • no, $team_code is the value of the field. $post_id is the post id.
    Isn’t that the correct structure?

  • A post object field stores a post ID. So, the $value of the field needs to be the related post object id and $post_id is the post you are updating. This is why I’m asking what the value of $team_code is.

  • This is the documentation I can find for update field
    https://www.advancedcustomfields.com/resources/update_field/
    is it different to the post object type?
    How can I pass the $team_code value, which is the value I want to the field and the other necessary information ie the post object id?
    Thanks

  • When using update_field() you must supply the actual value that ACF stores. For example the documentation shows how to update a checkbox field with an array. In the case of fields types like a post object field, ACF stores the ID of that post object and not the actual post object. So if you are supplying the entire post object in the updated then ACF does not know what to do with it, or it is attempting to update the value and failing.

    I’m asking you what the value of $team_code is because I can’t tell this by looking at your code. I haven’t got a clue what $team_code = rgar( $entry, '10' ); does.

  • rgar() is a gravity form function. It takes the value from the form field. This is the value I would like to obtain. eg $team_code = 101.
    How do I populate an acf field in the database (and in the new post I am creating) with this value? The acf field is a post object.
    What is the correct way to do this?
    Thanks

  • if the acf field you’re trying to populate is not a post object field then what kind of field is it? OP said it is a post object field.

  • Yes, it is a post object field. Sorry if that wasn’t clear.
    I need to take the value of the $team_code and put it into the post object field in my post.

  • As long as $team_code is a post ID (Integer) that represents the post you want to set the field and $post_id is the post ID of the post where you want to set this value and you use the field key for the field and this field is not some type of a sub field, like a field in a group field, or a clone field, or a repeater or flex field then

    
    update_field($field_key, $team_code, $post_id);
    

    should be updating the field.

    If the field is being set in the DB and your not seeing it on the front end, do you see this change in the admin?

    Do you have any caching plugins installed that would prevent this change being reflected on the front end?

  • The $test_code is the VALUE of the field I want to populate, not a post_id. Do I need to send an array as the $test_code which includes the value and a post_id?
    I’m not sure what ‘post ID (Integer) that represents the post you want to set the field’ means – what post_id should be here? can you please clarify?
    $post_id is the post_id of the post where I want to populate the value.

  • I finally figured this out. I needed to do
    update_field( 'field_59f6d4dd91fcd', $post_id_of_post_I_want_to_connect_to, $post_id );
    This gives me the post_title as the post_object. Is there a way to populate the field – field_59f6d4dd91fcd with a different field within that post?
    To clarify – I have a post that has a acf field ‘team_code’. This is the value I want to set as the value for field field_59f6d4dd91fcd. I am now getting just the title of the post that includes the team_code. How do I get the actual text value of team_code?

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

The topic ‘update field is updating value in database but on the frontend’ is closed to new replies.