Support

Account

Home Forums ACF PRO update_field / update_sub_field not working in custom rest-api endpoint

Solving

update_field / update_sub_field not working in custom rest-api endpoint

    • dankurf

    • November 17, 2020 at 12:06 pm

    I am trying to update a sub field in a custom rest api endpoint.

    Structure of my acf is as follows:

    
            "fields": [
                {
                    "key": "field_5faa2483959b9",
                    "label": "field group",
                    "name": "field_group",
                    "type": "group",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": [
                        [
                            {
                                "field": "field_5faa23d8959b8",
                                "operator": "==",
                                "value": "content"
                            }
                        ]
                    ],
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "layout": "block",
                    "sub_fields": [
                        {
                            "key": "field_5fadbecbfcc8a",
                            "label": "field_1",
                            "name": "field_1",
                            "type": "post_object",
                            "instructions": "",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "post_type": [
                                "writer"
                            ],
                            "taxonomy": "",
                            "allow_null": 0,
                            "multiple": 0,
                            "return_format": "id",
                            "ui": 1
                        },
                        {
                            "key": "field_5fa0795d696df",
                            "label": "field_2",
                            "name": "field_2",
                            "type": "text",
                            "instructions": "",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "",
                            "prepend": "",
                            "append": "",
                            "maxlength": ""
                        }
                    ]
        

    This is the callback I am running on the endpoint:

    function assign_subfield_val( $request ) {
    
    $parameters = $request->get_params();
    $project = $parameters['project']; //number
    $writer = $parameters['writer']; //number
      if($writer != '' && $project != ''){
    
    	update_sub_field(array('field_5faa2483959b9', 1, 'field_5fadbecbfcc8a'), $writer, 2527);
    	
    	return gettype($project);
      }else{
    	  return $parameters;
      }
    
    }

    I have use update_field and update_sub_field, used both names and IDs, nothing is working.

    If I try to update a top-level field this way it works, but having issues with the sub field. Please help!

  • Does row 1 of the repeater already exist?

    is $writer the post ID of the post you trying to set the relationship to?

    • dankurf

    • November 18, 2020 at 3:11 pm

    Actually my mistake was this was not a repeater field. What I ended up doing was

    `
    update_post_meta($project, ‘project_type_content_content_external_writer’, $writer);
    `

    Which appears to work fine.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.