[英]Wordpress: Update Custom Field using API Javascript
I am trying to update a custom field using javascript and the Wordpress REST API. 我正在尝试使用javascript和Wordpress REST API更新自定义字段。
I can easily create a new post using this and it works perfectly: 我可以使用它轻松地创建一个新帖子,并且效果很好:
var post = new wp.api.models.Post( { title: 'This is a test post' } );
post.save();
However I need to update a posts custom field. 但是,我需要更新帖子自定义字段。 I have tried the following code seen here: https://wordpress.stackexchange.com/questions/218261/wp-api-js-backbone-client-how-to-update-post-meta :
我已经尝试过以下代码: https : //wordpress.stackexchange.com/questions/218261/wp-api-js-backbone-client-how-to-update-post-meta :
var parentId = 91; // the post id
var metaData = new wp.api.collections.PostMeta('', {parent: parentId});
metaData.fetch()
.done(function(data) {
var someKey = data.findWhere({key: 'someKey'});
someKey.set('value', 'newValue');
someKey.save({parent: parentId});
});
but I just get the following error in the console: 但我只是在控制台中收到以下错误:
Uncaught TypeError: wp.api.collections.PostMeta is not a constructor
Any assistance would be great. 任何帮助都会很棒。
Thanks 谢谢
NOT A SOLUTION AN ALTERNATIVE 不是解决方案
I gave up on this method and instead went a different route using Ajax as outlined here: https://teamtreehouse.com/community/how-update-custom-fields-with-ajax-in-wordpress 我放弃了这种方法,而是使用了Ajax的不同方法,如下所示: https : //teamtreehouse.com/community/how-update-custom-fields-with-ajax-in-wordpress
Basically jQuery: 基本上是jQuery:
jQuery.ajax({ // In Wordpress use jQuery rather than $ to avoid conflict
url : 'https://example.com/wp-admin/admin-ajax.php', // This address will redirect the query to the functions.php file, where we coded the function that we need.
type : 'POST',
data : {
action : 'my_action',
postid : postid,
},
beforeSend: function() {
//console.log('Updating Field');
},
success : function( response ) {
},
complete: function(){
}
});
Functions.php Functions.php
function my_action(){
$order_id = $_POST['postid'];
// DO something
echo $return;
wp_die($order_id = ''); // this is required to terminate immediately and
return a proper response
}
I will not mark this as the solution as it isn't but its here to help anyone who comes across it. 我不会将其标记为解决方案,因为它不是,但在这里它可以帮助遇到它的任何人。
Suppose you have a custom taxonomy Vehicles 假设您有一个自定义分类法车辆
var vehicles = new wp.api.collections.Vehicles();
vehicles.create(
{
name: 'Truck',
description: 'A truck or lorry is a motor vehicle designed to transport cargo'
meta:{
'_wiki_link': 'https://en.wikipedia.org/wiki/Truck'
}
});
see wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php:437
参见wp-includes / rest-api / endpoints / class-wp-rest-terms-controller.php:437
add_action( 'rest_insert_vehicles', function( $term, $request, $flag ){
$term_meta = $request->get_json_params()['meta'] ?? [];
foreach( $term_meta as $meta_key => $meta_value ){
update_term_meta( $term->term_id, $meta_key, $meta_value );
}
}, 20, 3 );
Had a similar problem, trying to update the term meta from the javascript rest api... 遇到了类似的问题,尝试从javascript rest API更新术语meta。
and it seems as though you CANNOT do it purely with javascript... 似乎您不能完全使用javascript做...
reason being is, once the term is inserted via the rest terms controller (snippet below) 原因是,一旦通过其余术语控制器插入了该术语(下面的代码段)
see wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php#L428
参见wp-includes / rest-api / endpoints / class-wp-rest-terms-controller.php#L428
$term = wp_insert_term( wp_slash( $prepared_term->name ), $this->taxonomy, wp_slash( (array) $prepared_term ) );
... the actual details of the term are NOT sent to the update portion of this controller ...术语的实际详细信息不会发送到此控制器的更新部分
see wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php#L444
参见wp-includes / rest-api / endpoints / class-wp-rest-terms-controller.php#L444
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
$meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
if ( is_wp_error( $meta_update ) ) {
return $meta_update;
}
}
You'll notice that it sends through the $request['id']
instead of the $term['id']
, assuming there's a reason for this (maybe you're performing an update instead of a create) 您会注意到,它是通过
$request['id']
而不是$term['id']
,这是有原因的(也许您执行的是更新而不是创建)
I may be wrong here... but this is what i've concluded 我在这里可能是错的...但这就是我的结论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.