acf-to-rest-api icon indicating copy to clipboard operation
acf-to-rest-api copied to clipboard

Post/Put ACF Fields not working

Open mbaljeetsingh opened this issue 7 years ago • 15 comments

Hi,

wordpress version: 4.9.1, plugin version: 3.1.0

I have created custom field type called Tasks And added a ACF field short_description to it.

If i add/ update the short_description in the tasks from wp-admin, I can see the result with short_description (ACF field) at the endpoint (wp-json/wp/v2/tasks).

I'm authenticated with JWT Auth Token plugin.

But the problem happens, I can create/ update task (using POSTMAN)

(POST) http://localhost/wordpress/wp-json/wp/v2/tasks/19

{
	"title" : "Postman 1",
	"acf" : {
		"short_description": "Again"
	},
	"status" : "publish"
}

It creates the Task, but with title only. Am I passing the ACF fields the right way to POST/ PUT request?

This is the one thing that is stopping me from using Wordpress as a complete API backend for any application.

mbaljeetsingh avatar Dec 08 '17 12:12 mbaljeetsingh

You should see POST working using Basic Authentication - I have not tried JWT Auth Token.

Can you get your request working via Postman?

Chris-AdHome avatar Jan 23 '18 18:01 Chris-AdHome

@Chris-AdHome I don't it is a good idea to use Basic Authentication in production.

And no, JWT Token wasn't working in Postman. Basic Auth Works.

mbaljeetsingh avatar Jan 24 '18 06:01 mbaljeetsingh

Hello @mbaljeetsingh, What is the structure of your data to create the task?

{
	"title": "Task 1",
	"fields": {
		"short_description": "Again"
	}
}

Thanks

airesvsg avatar Jan 24 '18 11:01 airesvsg

Hello @airesvsg,

Yes, the structure is like this.

mbaljeetsingh avatar Jan 24 '18 11:01 mbaljeetsingh

Hello @airesvsg , I'm running the latest wordpress version and I have install your plugin (request version 3).

I manage to mage GET request without any problem using both BASIC and token based auth.

However, I am unable to update my custom fields on existing posts. 😢

I'm running the POST/PUT/PATCH request on the following endpoint: /wp-json/acf/v3/posts/the_post_id_to_update

The custom field I'm trying to update is "ai_file" (which returns when I make a GET request)

Here is the response I get when I GET : { "acf": { "ai_file": "sdfsqdfsdfsqdfdsq" } }

When I POST/PATCH/PUT here is the response I get: { "code": "rest_forbidden", "message": "Sorry, you are not allowed to do that.", "data": { "status": 401 } }

Do you think you could help ? 🙏

Kind regards,

rfenaux avatar Sep 11 '18 19:09 rfenaux

Alright, I managed to solve my issue:

  1. I had to download the following plugin for some random reason (https://github.com/WP-API/Basic-Auth)
  2. I use PATCH or POST to the following endpoint : http://yoursite.com/wp-json/wp/v2/posts/id with the following json : { "fields": { "your_custom_field": "the_value" } }

NB : the endpoint mentioned in the doc of "ACF to REST API" (/wp-json/acf/v3/posts/id) does not work - it returns a 500 error

rfenaux avatar Sep 11 '18 23:09 rfenaux

I was able to successfully use this JWT plugin (https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/) and update the custom acf fields using the "fields" value.

Located the JWT token by issuing http://ew-localhost.com/wp-json/jwt-auth/v1/token POST content-type: application/json; charset=UTF-8 Pass in "username" "password" of Wordpress user to authenticate and receive Bearer token.

Defined a custom post type called courses using this plugin (https://wordpress.org/plugins/custom-post-type-ui/) and allow REST API access.

Example call to update the custom post type course, post id = 91 http://localhost/wp-json/wp/v2/course/91 POST content-type: application/json; charset=UTF-8 authorization: Bearer JWT token goes here

{
    "title": "This is a test title",
    "fields": {
      "course_short_description": "Example short description",
      "content_overview": "Sample content overview",
      "open_edx_meta": {
        "modified_by": [
          3
        ],
        "date_modified": "2018-10-12 23:10:26.139728"
      }
    }
}

ztraboo avatar Oct 17 '18 13:10 ztraboo

@ztraboo your method did work. I'm now confuse, if we make the request against the WordPress REST API, do we require this pluging at all ?

Answer: Yes, we do require the plugin, since it add the ability to update and get ACF fields from the normal WP V2 REST API.

So, install it, but use the WP V2 REST API instead

killua99 avatar Mar 05 '19 14:03 killua99

@killua99 I'm using the JWT token authentication instead of Basic Authentication (Wordpress user/pass) to authenticate a different way with the WP V2 REST API. You don't have to use the JWT plugin to authenticate but can instead use Basic Authentication if you want to. I just chose to use JWT for my application. That way if someone were to crack my user credentials using JWT they wouldn't have access to the Wordpress admin page but if they had Basic Authentication they would be able to do that.

ztraboo avatar Mar 07 '19 13:03 ztraboo

Hi,

wordpress version: 4.9.1, plugin version: 3.1.0

I have created custom field type called Tasks And added a ACF field short_description to it.

If i add/ update the short_description in the tasks from wp-admin, I can see the result with short_description (ACF field) at the endpoint (wp-json/wp/v2/tasks).

I'm authenticated with JWT Auth Token plugin.

But the problem happens, I can create/ update task (using POSTMAN)

(POST) http://localhost/wordpress/wp-json/wp/v2/tasks/19

{
	"title" : "Postman 1",
	"acf" : {
		"short_description": "Again"
	},
	"status" : "publish"
}

It creates the Task, but with title only. Am I passing the ACF fields the right way to POST/ PUT request?

This is the one thing that is stopping me from using Wordpress as a complete API backend for any application.

in json above use "fields" instead of "acf"

netsenior avatar Mar 08 '19 02:03 netsenior

@netsenior The documentation for ACF says to use "acf" setting but if you look up at my example here https://github.com/airesvsg/acf-to-rest-api/issues/196#issuecomment-430637497 you'll see that I'm using "fields" instead. Change that name to see what it does in your POST body JSON payload.

ztraboo avatar Mar 08 '19 13:03 ztraboo

Use the filter acf/rest_api/key to change the key fields.

add_filter( 'acf/rest_api/key', function( $key, $request, $type ) {
    return 'acf_fields';
}, 10, 3 );

Now, the fields should be sent into the key acf_fields.

airesvsg avatar Mar 08 '19 14:03 airesvsg

Hi,

wordpress version: 4.9.1, plugin version: 3.1.0

I have created custom field type called Tasks And added a ACF field short_description to it. If i add/ update the short_description in the tasks from wp-admin, I can see the result with short_description (ACF field) at the endpoint (wp-json/wp/v2/tasks). I'm authenticated with JWT Auth Token plugin. But the problem happens, I can create/ update task (using POSTMAN)

(POST) http://localhost/wordpress/wp-json/wp/v2/tasks/19

{
	"title" : "Postman 1",
	"acf" : {
		"short_description": "Again"
	},
	"status" : "publish"
}

It creates the Task, but with title only. Am I passing the ACF fields the right way to POST/ PUT request? This is the one thing that is stopping me from using Wordpress as a complete API backend for any application.

in json above use "fields" instead of "acf"

yess buddy you help me on this, thanks God bless!

okta2510 avatar Mar 26 '19 07:03 okta2510

Hi,

I have put:

add_filter( 'acf/rest_api/key', function( $key, $request, $type ) {
  return 'acf_fields';
}, 10, 3 );

in my theme function file, but I am still getting:

 "acf": {
        "paul": null,
        "custom": null
    },

acf and not acf_fields

Please could someone let me know how to fix? Thank you!

I have the latest version of wordpress and acf installed.

P4ul2000 avatar Sep 01 '19 18:09 P4ul2000

Guys, I have the same issue, seems that here is solved (I'm not sure if this video is in English too): https://www.miwordpress.es/%E2%9C%85-acf-en-el-api-de-wordpress-%F0%9F%9A%80%F0%9F%9A%80%F0%9F%9A%80/

The plugin that uses in the video: https://github.com/airesvsg/acf-to-rest-api/

ProtocolNebula avatar Apr 30 '21 14:04 ProtocolNebula