Home › Forums › Backend Issues (wp-admin) › Is it actually possible to populate custom fields via the REST API?
I’ve been trying for days to do this, including consulting AI’s through hundreds of chats and just cannot get results.
I’m trying to use the API to create a post within a CPT of Archetypes and also populate the custom fields with text. The only thing I’ve achieved is to make the post. The custom fields never populate.
Looking around the net I can see this has been an issue with many other people with no solutions posted. And yes, I’ve enabled to ‘show in REST API’.
I have same problem, added custom fields to media file, filled it an no data shows in wp-json/wp/v2/media/
Any idea ?
I manage to trace issue to code
//Filter results.
if ( $filter && $posts ) {
var_dump($filter);
return $this->filter_posts( $posts, $filter );
In the file class-acf-internal-post-type.php, within the get_posts function, I believe the post_id used in the filter is invalid, as it points to a media post ID rather than an ACF post. I don’t know the internals of ACF, but after removing it, the ACF values appeared in the JSON API. However, this may break other things.
Edit: there is invalid code in get_field_groups_by_id in rest api,
default case is invalid, and does not check for attachement sub type.
default:
$args = $object_sub_type === “attachment” ? array( ‘attachment’ => $object_id ): array( ‘post_id’ => $object_id );
For me this was caused by an issue with the ACF screen that was used during the REST requests missing the attachment key. Filtering the screen and adding for media posts worked for me
add_filter(‘acf/location/screen’, function($screen) {
if ($screen[‘post_id’]) {
$post = get_post($screen[‘post_id’]);
if($post && $post->post_type == ‘attachment’) {
$screen[‘attachment’] = $screen[‘post_id’];
}
}
return $screen;
}, 10, 1);
You must be logged in to reply to this topic.
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.