airesvsg / acf-to-rest-api Goto Github PK
View Code? Open in Web Editor NEWExposes Advanced Custom Fields Endpoints in the WordPress REST API
Home Page: https://wordpress.org/plugins/acf-to-rest-api/
Exposes Advanced Custom Fields Endpoints in the WordPress REST API
Home Page: https://wordpress.org/plugins/acf-to-rest-api/
Hi,
I've a question if it's possible to get specific data within the api of a page or post using endpoints? Like I have a page: http://mvnguyen.com/wp-json/acf/v2/page/4/ and all I want is the social_links repeater by just add on the field name like so: http://mvnguyen.com/wp-json/acf/v2/page/4/social_links. Or it's already there but I don't how to do it.
Thanks
Hi airesvsg, i've pretty much copied your js function to perform rest calls and also the php/html of the modal, all goes nice and smooth except when comes to updating checkboxes. If i check one of them when i create the post it works, if i want to modify the post i can check and the result is ok, but i'm unable to uncheck and send the status modification. Is there any shrewdness to implement to perform such task?
TY,
G.
Hey, I am trying to use your plugin, however in the wp_json it is returning acf: false for a custom post type that has custom fields. Any ideas on what I am missing?
Hi,
I've a question regarding updating ACF fields via POST request from third party clients.
If I curl
curl http://mydomain.com/wp-json/acf/v2/pages/32
I get everything as expected, but if I do a post like:
curl -X POST -H "Content-Type: application/json" -d '{acf:{"field":"2"}}' -u user:pw http://mydomain.com/wp-json/acf/v2/page/32 -v
I get error 500.
Any ideas what I'm doing wrong?
Kind regards
Hi,
Thanks for your great plugin !
Please could you tell me how we can get the draft content, with an URL like : /wp-json/wp/v2/posts/?status=draft ?
Indeed, for your information actually we use the plugin https://wordpress.org/plugins/live-drafts/ in order to have, in same time, 2 versions of a post : published & draft
But maybe you know the better way (new filter or other plugin ... ) to have :
Thanks by advance for your help
Best regards
When i try to get the data from the post type product, i receive a error 404, here is my code:
$.ajax({ url: {homeURL} + '/wp-json/acf/v2/product/' + {id}, type: 'GET' })
.done( function( data ) {
console.log('Product data, acf ' , data);
})
This is the error.
{"code":"rest_no_route","message":"No se encontr\u00f3 la ruta que coincida con la URL y el m\u00e9todo de la petici\u00f3n","data":{"status":404}}
I appreciate any help.
I'm seeing the following behaviour:
In order to be consumed easily by any REST API client, wouldn't it make more sense to return null for empty scalar and object fields and [] for empty array fields?
Or is there another way to rewrite those values?
Hi,
I've a question about ACF & oEmbed field.
Is there a way to retrieve media url from a Twitter status in an oEmbed field (by modifying/overriding the JSON response)?
Thanks for your feedback.
Kind regards
Hi, Thanks for your great code,
#9 Does this solution work also to get fields from /users/ ?
http://dev.dentrist.com/wp-json/acf/v2/article :
{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method",
"data": {
"status": 404
}
}
When checking http://dev.dentrist.com/wp-json/acf/v2/ :
{
"namespace": "acf/v2",
"routes": {
"/acf/v2": {
"namespace": "acf/v2",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"namespace": {
"required": false,
"default": "acf/v2"
},
"context": {
"required": false,
"default": "view"
}
}
}
],
"_links": {
"self": "http://dev.dentrist.com/wp-json/acf/v2"
}
},
etc .....
"/acf/v2/article/(?P<id>\d+)/?": {
"namespace": "acf/v2",
"methods": [
"GET",
"POST",
"PUT",
"PATCH"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": []
},
{
"methods": [
"POST",
"PUT",
"PATCH"
],
"args": []
}
]
},
etc .....
While the regular WP REST API request works for GET:
http://dev.dentrist.com/wp-json/wp/v2/article
Thanks
Somehow whenever I hit this URL: http://staging2.marketplace.intelledox.com/wp-json/acf/v2/post/5225/
I was expecting all the fields in acf but only {"acf":false} was returned. Please help
"Doc_documento" is a custom post_type"
Every time i try to update with POST this url:
wp-json/wp/v2/doc_documento/1
The response is [acf] => stdClass Object ( [empty] => '' inside the object of that particular post.
Do you know why?
Hi!
Any experience with this or tips? We get an error when trying to do this and all the other ID's in the repeater array gets deleted.
In this example we wan't to PATCH an image field in a ACF repeater through the API.
Hi there.
How can I replace dash in the field name? I mean is there any filter to use this php function str_replace("-", "_", $field)?
So, If I have a field called (my-category).. I want it to be my_category when it is displayed in API
How to change the "acf" in the json response for base dictionary of acf fields?
Hello.
When trying to create a user and appending ACF fields i cant get it to work.
looking at ur example
<form action="http://localhost/wp-json/wp/v2/posts/1" method="POST">
<?php
// http://v2.wp-api.org/guide/authentication
wp_nonce_field( 'wp_rest' );
?>
<label>Title: <input type="text" name="title"></label>
<h3>ACF</h3>
<label>Site: <input type="text" name="fields[site]"></label>
<button type="submit">Save</button>
</form>
i tried the same but with users instead
<form action="http://localhost/v3/wp-json/wp/v2/users" method="POST">
<?php
// http://v2.wp-api.org/guide/authentication
wp_nonce_field( 'wp_rest' );
?>
<label>username: <input type="text" name="username"></label>
<label>email: <input type="text" name="email"></label>
<label>password: <input type="password" name="password"></label>
<h3>ACF</h3>
<label>phone: <input type="text" name="fields[phone]"></label>
<button type="submit">Save</button>
</form>
The user is created but no acf data is stored, there is a custom field created and i have tried alot of different rule types.
@airesvsg when I request the URL for my custom term:
/wp-json/wp/v2/{taxonomy}
does not come to acf key in json.
and when I request the URL your endpoints:
/wp-json/acf/v2/term/{taxonomy}/{id}
the acf key appears.
Can you help me?
Tks.
Is there a way to edit how the acf fields show in the JSON listing? For example I do not want my custom field to show under the field 'acf' but something custom. With the old plugin I was able to find how to change this but with V2 I am unable to find where this is set.
Hi,
But we have 2 issues :
1/ with the cache the fields we want to remove are not removed (it's OK without cache).
Indeed the cache plugin store the complete JSON and not the JSON filtered by our method add_filter('acf/rest_api/post/get_fields', function ($data) {...}
How can we correct that ?
2/ We have issue with performance when we search a new word (not cached) : about 3 seconds on our localhost with only 5 contents.
Do you have a solution ?
For example, I think it will be better to select in the query only fields needed instead of select all fields and remove fields in the JSON after ?
So, do you know how improve the search ?
Thanks by advance for you help.
Best regards
When including ACF this way https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/ I can't get any of the endpoints to work. When I disable loading ACF bundled inside my theme and add the plugin through the plugins menu in the WordPress admin the endpoints work.
The plugin is not retrieving all fields, only relational/post object fields. Is that a problem?
Ex:
Text, URL and other simple fields don't get up;
I'm getting always {"acf":false} when i access this route /wp-json/acf/v2/options
When I access the normal json api it works /wp-json/wp/v2/posts
Can you help me please?
I'm building a plugin that converts a blank WP to a mobile application backend in one click. To make things easy for me I'm using a combo of acf-pro
, the official wp-api
plugin and your acf-to-rest-api
plugin. I'm loading all these three plugins inside my plugin and this confuses the is_plugin_active()
function in the acf-to-rest-api
plugin. The current function tries to look for acf-pro
and wp-api
by using the WP is_plugin_active()
function that checks if the required plugins are activated but only based on the hard coded paths.
Could the "existence" of these plugins be checked in a more general way? My suggestions would be to use something like class_exists('acf')
and class_exists('WP_REST_Controller')
instead of the is_plugin_active()
and hard coded plugin paths. My case might sound a bit special but f.ex. including ACF to a theme is a very popular practice.
I can ofc fork this repository and do the changes I need but I'd prefer not to do such a thing if we could try and find a solution that would fit everyone and get merged to this amazing plugin. To make this easy for you I can prepare a pull request if we can find and agree on a solution.
Hi @airesvsg,
do you know how to retrieve posts in draft status?
I've tried ?status=draft but have an error (400 "rest_invalid_param").
I use Live Drafts plugin (https://wordpress.org/plugins/live-drafts/), WP OAuth Server (https://wp-oauth.com/), create a clientKey and clientSecret, generate a valid access_token and pass it to the webservice (/wp-json/wp/v2/posts/?status=draft&access_token=XXX).
Thanks for your help!
I'm using bucket theme (if it is helpful) and it comes with acf pre bundled and acf to rest api plgin showing "activate advanced custom fields" and it is not activating, how can i possibly solve this,
Hi everyone.
How are you? Hope you are doing well.
I have A question about filtering in ACF to REST API plugin.
Can I filter posts by custom field of an array?
See this please: http://i.imgur.com/u2eQydP.png
I want to filter posts that has specific id in anime-related field.
Hope you understand me.
Thank you for your time.
Hi, thank you for this great plugin. We're using WP-API 2, WPML and ACF together and I seem to have found an issue.
When you prefix the endpoint with a language code, the WP query is automatically changed e.g.
/wp-json/wp/v2/posts
will return all posts in the default language/es/wp-json/wp/v2/posts
will return all Spanish posts…however, the language prefix also breaks ACF. The fields are not included and in the JSON we see acf: false
.
Any ideas? Could this plugin somehow make sure to always add the ACF fields, even with a language prefix in the URL? As far as I can tell, this is the line returning null
when there's a language prefixed:
Hi,
Great Plugin!
Can you add /wp-json/acf/v2/categories (like: /wp-json/wp/v2/categories)?
http://v2.wp-api.org/reference/categories/
This is kind of an annoying question, and I apologize for that. I have a custom field on a post type (events) that is a post_object of another post type (documents). That documents post type also has its own set of fields.
When I access the documents field on the events post through the API, I get the document post object but it does not have the document ACFs associated with it. I'm can't figure out if this is a hook or a filter I can access via your plugin, or if the proper answer is somewhere else in the REST API plugin or in ACF's hooks.
Thanks for any help you can give, even if the answer is just, "Nope, not here!"
I have a custom post type ‘Files’ and have the plugins, ‘ACF to REST API’, ‘Advanced Custom Fields’ and ‘WP REST API’ installed.
I can get the JSON for ‘FILES’ using:
http://mywebsite.co.uk/wp-json/wp/v2/files/
I need to filter this using the custom fields set using ACF.
I can’t seem to find how I would do this and also I can’t seem to get any results via the endpoints specified by this plugin ie.
http://mywebsite.co.uk/wp-json/acf/post/ID
Can anyone help? I have spent all morning trying to get this working but am getting nowhere.
Many thanks
We are using this plugins and it rocks but we ran into problem where we need to search the posts any a custom field and I haven't found an answer anywhere. It would be a great feature to complement the plugin with.
Hello, thanks for the great plugin.
I have a custom field "rating" which is assigned to every comment,
I can read the value by doing :
GET http://website.com/wp-json/acf/v2/comment/2
and the response is
{
"acf": {
"rating": "3"
}
}
I need to update this value when a user submit a rating and the call looks like this:
PUT http://website.com/wp-json/acf/v2/comment/2
body is form-data
fields[rating] = 4
this is the response i get:
{
"code": "cant_update_item",
"message": "Cannot update item",
"data": {
"status": 500
}
}
Hi @airesvsg,
sorry to bother you again!
I have a new problem: the cache management.
How can I cache the json response (Redis, Memcache, ...). I've tried several plugins and librairies (Simple Cache, Redis Object Cache, jp-rest-cache) with no result.
Thanks
Hello,
is there anything planned on ACF Pro support for the REST API?
Thank you,
Sissi
I have a custom post type (Orders) which has custom fields and another custom post type called (address) which also has custom fields, when you add a new order it will ask you to choose an address (post object), When i call the posts endpoint of the type (Orders), it gets only the core fields of (address) post object but i cannot see the (address) custom fields
Hi there,
I'm trying to get ACF for the taxonomies. But I can't figure out how.
I've tried /wp-json/acf/v2/term/category/10, did not work. Also tried with the slug /category/{slug} and I'm getting an 301.
Any suggestions, please?
Thanks for the amazing work.
So this plugin looks great, but is there any authentication?
It kind of looks like if this plugin is activated, anybody could change whatever data they want to in the database. Unless the edit capabilities are disabled by default...
I just didn't see anything in the docs, thanks!
Hey! Thanks for making this plugin better and better!
I've just tried out updating of a post and found out that it doesn't work for boolean fields!
I've got a field called "cal_status", which is a boolean and I can't set it to "true".
Fields that are "text/string"-fields are working!
Cheers,
Lukas
Is there a specific beta version of WPI API that needs to be used with this plugin? At the moment, the endpoint serves up the homepage instead of the actual JSON data.
Thoughts?
http://dev-twg-express.pantheon.io/wp-json/acf/v2/post/
Hi Aires,
I'm trying to filter by ACF Field Value, just the same question asked for the previous version at airesvsg/acf-to-wp-rest-api#6.
Will your answer still work for this version? I tried your code but I'm not able to filter, getting all the results
I'm using:
Without plugin and no ACF fields = instant
Using: /wp-json/acf/v2/post/{id} OR /wp-json/wp/v2/post/{id}
With plugin and a couple of meta fields and repeaters = 25 seconds
Using: /wp-json/acf/v2/post/{id} OR /wp-json/wp/v2/post/{id}
Any chance to tweak the code for this proccess?
I've installed the latest WP REST API v2.0-beta13 and ACF to REST API v2.2.0 and I can't seem to get the request to return a response with the /acf/v2 URL.
I'm trying to get the the ACF Pro fields for a custom post type. This example works (without the custom fields obviously):
http://whiskynotes.co/wp-json/wp/v2/note/31
and this one doesn't:
http://whiskynotes.co/wp-json/acf/v2/note/31
I've added show_in_rest to the custom post type and have no other supporting functions for this. I feel like this should be something simple but I just can't figure it out. Please help!
https://github.com/starfishmod/WPAPI-SwaggerGenerator
Noted in that plugin: "This plugin works well with WP-APIv2 and any plugin that uses the WP_REST_Server class to register it's API's"
Hello again,
I create custom posts via php and also set some values to acf fields. In the admin of wordpress all fields are filled correct. If I access the post via the API I get only field or sometimes a field from a different post type.
If I save the (same) post in the backend then all fields are shown correct. Is there a special hook that triggers the exposure of the fields to the API which is ignored when they are created outside of Wordpress?
Best,
Sissi
Hi,
Can you add READABLE with some "general" options like:
/wp-json/wp/v2/posts
/wp-json/wp/v2/pages
/wp-json/wp/v2/categories
/wp-json/wp/v2/media
etc...
Is it possible to group fields in a post's JSON by the ACF field group? When I request my post's JSON through wp-api i get a flat list of fields underneath acf
. I need something grouped so i can determine what module to display in my front-end but also there is a practical concern of fields from different field groups with the same name overriding each other in the output.
Installed ACF TO REST API plugin on fresh install of wordpress with 2 acf fields created.
Hi @airesvsg,
Can you give me an example how to put/post data using the plugin. I want to using AJAX to post. I have an example code, but it didn't work. Maybe you can take a look.
$( '#post-submission-form' ).on( 'submit', function(e) {
e.preventDefault();
var title = $( '#post-submission-title' ).val(),
content = $( '#post-submission-description' ).val(),
username = $('#post-submission-username').val(),
display_name = $('#post-submission-display_name').val(display_name),
categories = $('#post-submission-category').val(),
tags = $('#post-submission-tag').val(),
featured_image = $('#post-submission-image').val(),
copyright = $('#post-submission-copyright').val(),
website = $('#post-submission-website').val(),
status = 'publish';
var data = {
title: title,
content: content,
// ACF Fields:
username: username,
display_name: display_name,
category: categories,
tag: tags,
featured_image: featured_image,
copyright: copyright,
website: website,
status: status
};
$.ajax({
method: "POST",
url: POST_SUBMITTER.root + 'wp/v2/posts',
data: data,
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce );
},
success : function( response ) {
console.log( response );
alert( POST_SUBMITTER.success );
},
fail : function( response ) {
console.log( response );
alert( POST_SUBMITTER.failure );
}
});
});
If I create a post type like this:
register_post_type('story', [
...
'rest_base' => 'stories',
...
]);
Then the Wordpress API routes look like this: /wp/v2/stories/:id
But this plugin uses the post type name to make the routes, so they look like this: /acf/v2/story/:id
The ACF routes should use the rest_base
of the post type to generate the routes. This also makes dealing with the attachment routes easier, because their rest_base
is already set to media
(see class-acf-to-rest-api.php#L53)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.