admin管理员组文章数量:1426026
I am trying to update a custom post type metadata on the front end using JS and the WP Rest API.
The object key is wishlist_array
under metadata
Here is how JSON response from /wp-json/wp/v2/oha_wishlist
(my custom post) looks:
{
"id": 1248,
.
.
.
"metadata": {
"wishlist_array": [
""
],
.
.
.
On php I set the register_rest_field()
function as follow:
register_rest_field('oha_wishlist', 'metadata', array(
'get_callback' => function ($data) {
return get_post_meta($data['id'], '', '');
},
'update_callback' => function ($data) {
// Don't understand how to use the arguments here
// How can I print or log the $data content??
return update_field($??, $??, $??);
},
And finally my fetch()
function where I connect to the API. It returns no error and if I try to update the title, for instance, it works well. Only doesn't when trying to update the metadata
.
fetch("/wp-json/wp/v2/oha_wishlist/1248", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-WP-Nonce": phpVarObj.nonce
},
body: JSON.stringify({ metadata: { wishlist_array: "add this" } })
})
.then(function(response) {
return response.json();
})
.then(function(wishlistPosts) {
console.log(wishlistPosts);
});
I am trying to update a custom post type metadata on the front end using JS and the WP Rest API.
The object key is wishlist_array
under metadata
Here is how JSON response from http://ohalocal.local/wp-json/wp/v2/oha_wishlist
(my custom post) looks:
{
"id": 1248,
.
.
.
"metadata": {
"wishlist_array": [
""
],
.
.
.
On php I set the register_rest_field()
function as follow:
register_rest_field('oha_wishlist', 'metadata', array(
'get_callback' => function ($data) {
return get_post_meta($data['id'], '', '');
},
'update_callback' => function ($data) {
// Don't understand how to use the arguments here
// How can I print or log the $data content??
return update_field($??, $??, $??);
},
And finally my fetch()
function where I connect to the API. It returns no error and if I try to update the title, for instance, it works well. Only doesn't when trying to update the metadata
.
fetch("http://ohalocal.local/wp-json/wp/v2/oha_wishlist/1248", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-WP-Nonce": phpVarObj.nonce
},
body: JSON.stringify({ metadata: { wishlist_array: "add this" } })
})
.then(function(response) {
return response.json();
})
.then(function(wishlistPosts) {
console.log(wishlistPosts);
});
Share
Improve this question
asked Jun 15, 2019 at 14:52
stemonstemon
1551 silver badge8 bronze badges
1 Answer
Reset to default 0If you're supplying the request data/body like so, which is an array of metadata:
{ metadata: { wishlist_array: "add this", foo: "bar baz" } }
and you want to update only the wishlist_array
metadata, then you could do:
// In your case, $field_value is the `metadata` value and $data is a WP_Post object.
'update_callback' => function( $field_value, $data ){
if ( is_array( $field_value ) && isset( $field_value['wishlist_array'] ) ) {
update_post_meta( $data->ID, 'wishlist_array', $field_value['wishlist_array'] );
return true;
}
}
Or if you want to update all metadata (or all fields supplied in that metadata
array), then this would do it:
// In your case, $field_value is the `metadata` value and $data is a WP_Post object.
'update_callback' => function( $field_value, $data ){
if ( is_array( $field_value ) ) {
foreach ( $field_value as $key => $value ) {
update_post_meta( $data->ID, $key, $value );
}
return true;
}
}
And regarding this question: "// How can I print or log the $data content??", you could do var_dump( $data );
and check the PHP error logs — or wp-content/debug.log
if it is enabled.
Some Notes
In the
update_callback
function, the$field_value
is the value of themetadata
parameter; whereas the$data
is the data object — this is equivalent to the$data
in theget_callback
function, except that in theupdate_callback
function, the$data
is not an array.You may also want to check this sample code on the REST API handbook.
The examples above are basic examples and as stated on the REST API handbook, carefully consider what permissions checks or error handling may be required for your specific field.
With
update_field()
(ACF), you could also use$data->ID
as the third parameter passed toupdate_field()
.
本文标签: wp apiUpdate Custom Post Type Metadata Wp Rest API
版权声明:本文标题:wp api - Update Custom Post Type Metadata Wp Rest API 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745396599a2656842.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论