ACF Update_field() update the field but changes are not seen in backend or rest api

admin2025-01-07  3

I have a custom field for User type where I can link an attachement ID. I'm trying to save the attachment ID programmatically and it looks like it works, because when I run get_field($my_field) I have the right value. However the changes are not seen in the backend or in rest api, it still show the old value. I'm baffled, what could I be doing wrong?

Here is my code:

//Update field with attachment ID and user_id
$cover_custom_field_key = "field_5e9ed645b9ec3"; //cover field
$cover_id = media_handle_sideload( $image_file, 0 );
update_field( $cover_custom_field_key, $cover_id, 2 ); //update field for user id=2

echo get_field($cover_custom_field_key, 2); // echo : /

But in the backend there is still the old link to another image, and in the rest response for wp-json/wp/v2/users/2 also:

    "id": 2,
    "name": "Mata Hari",
    "url": "",
    "description": "",
    "acf": {
        "practice_playlist": false,
        "cover": "/"
    }

I have a custom field for User type where I can link an attachement ID. I'm trying to save the attachment ID programmatically and it looks like it works, because when I run get_field($my_field) I have the right value. However the changes are not seen in the backend or in rest api, it still show the old value. I'm baffled, what could I be doing wrong?

Here is my code:

//Update field with attachment ID and user_id
$cover_custom_field_key = "field_5e9ed645b9ec3"; //cover field
$cover_id = media_handle_sideload( $image_file, 0 );
update_field( $cover_custom_field_key, $cover_id, 2 ); //update field for user id=2

echo get_field($cover_custom_field_key, 2); // echo : https://example.com/cover-image/

But in the backend there is still the old link to another image, and in the rest response for wp-json/wp/v2/users/2 also:

    "id": 2,
    "name": "Mata Hari",
    "url": "",
    "description": "",
    "acf": {
        "practice_playlist": false,
        "cover": "https://example.com/abstract-colorful-geometric-banner-template-background/"
    }
Share Improve this question asked Apr 21, 2020 at 12:53 KalianeyKalianey 1758 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Ok got it, for user field actually one has to add "user_" before the id for it to work. So in my case:

update_field( $cover_custom_field_key, $cover_id, "user_2" ); 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260255a652.html

最新回复(0)