【问题标题】:Wordpress: Update Custom Field using API JavascriptWordpress:使用 API Javascript 更新自定义字段
【发布时间】:2018-07-22 19:41:20
【问题描述】:

我正在尝试使用 javascript 和 Wordpress REST API 更新自定义字段。

我可以使用它轻松创建一个新帖子,并且效果很好:

var post = new wp.api.models.Post( { title: 'This is a test post' } );
post.save();

但是我需要更新帖子自定义字段。我已经尝试过这里看到的以下代码: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});
});

但我只是在控制台中收到以下错误:

Uncaught TypeError: wp.api.collections.PostMeta is not a constructor

任何帮助都会很棒。

谢谢

【问题讨论】:

    标签: javascript jquery wordpress rest api


    【解决方案1】:

    不是替代方案

    我放弃了这种方法,而是使用 Ajax 采取了不同的路线,如下所述:https://teamtreehouse.com/community/how-update-custom-fields-with-ajax-in-wordpress

    基本上是 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(){
    
                }
    });
    

    函数.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
    } 
    

    我不会将此标记为解决方案,因为它不是,但它在这里帮助遇到它的任何人。

    【讨论】:

      【解决方案2】:

      TLDR;

      假设您有一个自定义分类车辆

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

      在你的 php 中

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

      遇到了类似的问题,尝试从 javascript rest api 更新术语 meta...

      似乎你不能纯粹用 javascript 来做......

      原因是,一旦通过其余术语控制器(下面的 sn-p)插入术语

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

      ...该术语的实际详细信息不会发送到此控制器的更新部分

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

      你会注意到它通过$request['id']而不是$term['id']发送,假设这是有原因的(也许你正在执行更新而不是创建)

      我在这里可能是错的......但这就是我的结论

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-30
        • 2017-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-25
        相关资源
        最近更新 更多