Home › Forums › Feature Requests › REST API
Hi @effigy,
Now you can edit the ACF fields using WordPress REST API.
https://github.com/airesvsg/acf-to-rest-api
Hi Airsvsg
Finally got back onto this…
I am trying to post some fields into an existing custom post type that already has ACF fields. But I’m getting a 500 error…
{“code”:”cant_update_item”,”message”:”Cannot update item”,”data”:{“status”:500}}
This is what I am using… Am I missing something?
{“acf”: {
“status”: “active”,
“SAPCode”: “123456”,
“SublessorSAPCODE”: “345678e”,
“publicliabilityinsurance”: “23-06-2016”,
“workCoverInsurance”: “20-08-2016”,
“Type”: “contractor”,
“subTypeContractor”: “plumbing”,
“Email”: “[email protected]”,
“phone”: “07 3725 6100”,
“facsimile”: “”,
“afterHoursTelephone”: “”,
“Location”: “K1, South Gate East Commercial Centre”,
“tenantAdministrator”: “”,
“companyWebsite”: “”
}
}
Hi Airsvsg
I seems to have resolved my issue.
I added this from your documentation to the function.php…
add_filter( 'acf/rest_api/key', function( $key, $request, $type ) {
return 'acf_fields';
}, 10, 3 );
Then changed ‘acf’ to ‘acf_fields’ at the top of the JSON as follows…
{“acf_fields”: {
“status”: “active”,
“SAPCode”: “123456”,
“SublessorSAPCODE”: “345678e”,
“publicliabilityinsurance”: “23-06-2016”,
“workCoverInsurance”: “20-08-2016”,
“Type”: “contractor”,
“subTypeContractor”: “plumbing”,
“Email”: “[email protected]”,
“phone”: “07 3725 6100”,
“facsimile”: “”,
“afterHoursTelephone”: “”,
“Location”: “K1, South Gate East Commercial Centre”,
“tenantAdministrator”: “”,
“companyWebsite”: “”
}
}
Works like a charm.
Cheers
Hi @airesvsg,
I’m having a problem displaying the acf fields in the api from dynamically created posts using wp_insert_post()
(see WPSE question here ) I’m guessing I need to make the WP API writable to the ACF fields. They do display once I click the Publish button though but there are too many posts to do this.
Cheers
Looking at your code over there, the biggest thing that I see when it comes to ACF is that you’re using the field name when updating the ACF fields.
When a post and the ACF field values does not previously exist, using update_field() using the field name will fail because ACF does not know what to do with the value.
When using update_field() in this situation you need to use the field key instead of the field name.
You could also use update_post_meta() and add everything that ACF will need. When ACF adds a value to the field it inserts 2 rows in the database:
meta_key | meta_value
{$field_name} | $value
_{$field_name} | $field_key
Thanks for your response @John_Huebner, I also tried this with
update_post_meta()
and didn’t have any success in displaying the data. That said, even without the update functions the post meta is inserted into the database. It is only the API that isn’t being updated. I’ll remember that when using update_field()
though.
Cheers
I’m not sure what 2 of these fields are from your code.
$meta1 = $obj['taxonomy'][0];
$meta2 = $obj['nom'];
$meta3 = $obj['prenom'];
The first one looks like a taxonomy field. If this is a problem with ACF displaying this value correctly then it’s more than likely related to the field key not being inserted into the database. Without that field key ACF does not know that it’s a taxonomy field and will treat it as a simple text field when it gets the value from it. This is the case for several types of fields in ACF. Basically, if the field stores anything but a simple text value it will always have a problem if it can’t find the field key that matches the field value in the DB.
You also mention this
The post meta is indeed imported but I have to manually click the update button in order to display the data on the front-end.
and that is another symptom of the field key missing in the DB.
Although I could be mistaken about what API your referring to, I am assuming that you mean ACF displaying values on the front end of the site.
The json data that I am importing is exported from a site using Drupal6. In the orginial code Drupal considered this to be a taxonomy but in WordPress it is a simple text field as are the other two meta fields. The WordPress site uses the wp API (v2) to display the post data on the front end for reasons that are a bit complicated to explain 😉
I take your point that the field key is required though. So I take it I should retrieve the field keys of each of the fields in question and also update them in my import function?
update_post_meta( $post_id, _{$field_name}, $field_key )
I would, at least to eliminate it as a possible cause of the problem. I’m not sure you can actually look up the field key, you may need to hard code those the same way that you’re doing it with the meta keys.
Well, @John, you were dead right. I looped through and updated the field keys and that did the trick 🙂 Cheers for all your help 🙂
Hi – I am currently tring to add category images to the rest response – I have tried seceral plugins that states to do so, but have not been successful. Therefore i have now created a custom field in my categories with an image – It is saved etc – and on the rest of the site, it is all good.
When I try to find it in the response, it does not work though.
If I call something else, like recipies etc. it works fine and I see the ACF field – But calling /wp-json/wp/v2/categories/id does not show the “ACF” field –
What can i do?
Hello,
I’m using the theme videotube and I have a custom type post called “video”.
Also I have a advanced custom field called “video_url” that I would like to retrieve from the WordPress API REST.
Does your plugin allow me to do that?
Thanks for your help
Thanks but actually the plugin WP REST API Controller, works fine and provided exactly what I wanted: it allows me to expose data (like an advanced custom field from a custom post type) to the REST API.
Sorry, acf-to-rest-api did not work for me.
No, that plugin isn’t for exposes ACF fields in rest api.
I already studied the code of that plugin and it’s only to control the access.
Thanks
I’m the developer the plugin ( ACF to REST API ):
https://github.com/airesvsg/acf-to-rest-api
I studied that plugin ( WP REST API Controller ) to contribute.
https://github.com/yikesinc/wp-rest-api-controller
What’s the plugin that I need to study?
Thanks
Hi ,
I m facing an annoying problem. I want to update my custom field in javascript using REST API.I have gone through all steps that you show in the documentation. Here is the code snippet:
(Taskstatus is the custom field that I want to update)
function updateStatus(id) {
const payLoad = {
Taskstatus: “In process”
};
const postData = JSON.stringify(payLoad);
fetch(http://artingineer.dk/refarmedwordpress/wp-json/acf/v3/posts/${id}
, {
method: “put”,
headers: {
“Content-Type”: “application/json; charset=utf-8”,
Authorization:
“Bearer ” +
“eyJ0eXAiOiJKV1”
},
body: postData
})
.then(res => res.json())
.then(d => {
console.log(d);
});
}
Hi @airesvsg
I have installed your plugin in the hope to use it to fetch ACF data and set it accordingly.
I am can confirm the plugin is activated as I get the end points / routes when I use the https://www.MYSITEURL.com/wp-json/acf/v3/ URL.
However when I go to try and list all data for my custom post type “agents” I get a no such route. I used your endpoint from the endpoints guide /wp-json/acf/v3/{post-type}
So for me this was https://www.MYSITEURL.com/wp-json/acf/v3/agents/ and I get a response:
{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method",
"data": {
"status": 404
}
}
This is the same for all my custom post_types.
Did I miss something basic in the installation? Do I need to add something to my functions.php or add filters in there?
Any help would be great as if the plugin was to work it would be a real benefit.
Cheers,
Col
Just to add when I use the following to list posts: https://www.MYSITEURL.com/wp-json/acf/v3/posts
I get the following:
[
{
"id": 35980,
"acf": []
},
{
"id": 33675,
"acf": []
},
{
"id": 33220,
"acf": []
},
{
"id": 32808,
"acf": []
},
{
"id": 32006,
"acf": []
},
{
"id": 31846,
"acf": []
},
{
"id": 30240,
"acf": []
},
{
"id": 28420,
"acf": []
},
{
"id": 27843,
"acf": []
},
{
"id": 27531,
"acf": []
}
]
I am sure I have not configured something so some guidance might get me up and running on this one. Cheer, C
Hey all 👋
As part of the ACF 5.11 release we added native support for ACF fields in the WordPress REST API. Read more about that here.
The topic ‘REST API’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.