简体   繁体   English

WordPress:使用API​​ Javascript更新自定义字段

[英]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. 我不会将其标记为解决方案,因为它不是,但在这里它可以帮助遇到它的任何人。

TLDR; TLDR;

Suppose you have a custom taxonomy Vehicles 假设您有一个自定义分类法车辆

in your javascript 在你的JavaScript中

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'
    }
});

in your php 在你的PHP中

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.

相关问题 使用 Javascript 在 wordpress 中进行自定义字段计算 - Custom field calculation in wordpress using Javascript 使用 wordpress API 和纯 javascript 更新 wordpress 数据库 - Update wordpress database using wordpress API and pure javascript 在 Wordpress 页面上使用自定义 Javascript 更新 DIV 元素 - Update DIV Element Using Custom Javascript on a Wordpress Page WordPress自定义字段-Javascript中的CSS - Wordpress Custom Field - CSS within Javascript 如何使用 Xrm Web Api 更新 Microsoft 模型驱动应用程序中的自定义查找字段? - How to update a custom lookup field in a Microsoft Model-driven App using the Xrm Web Api? WordPress Rest API:按自定义字段值过滤帖子 - WordPress Rest API : filtering posts by custom field value 如何在JavaScript地图中使用Wordpress自定义字段变量? - How to use wordpress custom field variable in javascript map? 将自定义Wordpress帖子/页面元字段的值分配给JavaScript变量 - Assign Value of Custom Wordpress Post/Page Meta Field to JavaScript Variable Wordpress Rest Api 在自定义端点上使用 POST 更新内容 - Wordpress Rest Api Update content with POST on custom endpoint Liferay 7:使用javascript api jsonws检索自定义字段值,返回java.lang.NullPointerException - Liferay 7: Retrieved custom field value using javascript api jsonws return java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM