为wp rest API中的自定义帖子类型更新帖子元

时间:2019-07-20 16:01:32

标签: wordpress rest api advanced-custom-fields

我正在尝试通过WP Rest API为自定义帖子类型更新ACF帖子元字段。我已经按照SO给出了一些答案,但是我正在做的事情似乎没有任何作用,在这种情况下,我正在尝试更新assigned_volunteer发布元键/值。

当前正在更新除元字段以外的所有数据

这是我的功能文件:

/****************************************************************************************
* Add REST API support to an already registered post type
* http://v2.wp-api.org/extending/custom-content-types/
* Access this post type at yoursite.com/wp-json/wp/v2/post_type_name
****************************************************************************************/
add_action( 'init', 'appp_post_type_rest_support', 999 );
function appp_post_type_rest_support() {
  global $wp_post_types;
  //be sure to set this to the name of your post type!
  $post_type_names = array('volunteer_events','volunteers', 'member_reviews');
  foreach($post_type_names as $post_type_name){
    if( isset( $wp_post_types[ $post_type_name ] ) ) {
      $wp_post_types[$post_type_name]->show_in_rest = true;
      $wp_post_types[$post_type_name]->rest_base = $post_type_name;
      $wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
    }
  }
}
/****************************************************************************************
* Add post meta to a response
* http://v2.wp-api.org/extending/modifying/
* This example adds price to a woocommerce product. Meta key is _price, and corresponding api field will also be _price. 
****************************************************************************************/
// Uncomment the line below to enable this function.
add_action( 'rest_api_init', 'appp_register_post_meta' );
function appp_register_post_meta() {
    register_rest_field( 'volunteer_events', // any post type registered with API
        'description', // this needs to match meta key
        array(
            'get_callback'    => 'appp_get_meta',
            'update_callback' => null,
            'schema'          => null,
        )
    );

    register_rest_field( 'volunteer_events', // any post type registered with API
        'assigned_volunteer', // this needs to match meta key
        array(
            'get_callback'    => 'appp_get_meta',
            'update_callback' => function( $field_value, $data ){
                if ( is_array( $field_value ) ) {
                    foreach ( $field_value as $key => $value ) {
                        update_field( $key, $value, $data->ID );
                    }
                    return true;
                }
            },
            'schema'          => null,
        )
    );
}

下面是我的JS文件,如果传递的statusdeclined,那么我想删除assigned_volunteer的值。

//Handle Approve/Decline Buttons 
    $( '.volunteer-actions .action' ).on( 'click', function(e) {
        e.preventDefault();
        var id = $(this).attr('data-post');
        var status = $(this).attr('data-action');

        if(status == 'declined'){
            var data = {
                id: id,
                status: status,
                metadata: {
                    assigned_volunteer: '',
                }
            };
        } else {
            var data = {
                id: id,
                status: status,
            };
        }

        $.ajax({
            method: "POST",
            url: POST_SUBMITTER.root + 'wp/v2/volunteers/' + id,
            data: data,
            beforeSend: function ( xhr ) {
                xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce );
            },
            success : function( response ) {
                console.log( response );
                alert( POST_SUBMITTER.success );
                location.reload();
            },
            fail : function( response ) {
                console.log( response );
                alert( POST_SUBMITTER.failure );
            }

        });
});

0 个答案:

没有答案