Wordpress 使用元字段创建帖子–;WP-restapi

Wordpress 使用元字段创建帖子–;WP-restapi,wordpress,wordpress-rest-api,Wordpress,Wordpress Rest Api,我刚刚开始使用RESTAPI,并使用它从前端创建帖子。我设法发表了一篇有标题、摘录和内容的文章。 我想添加一个自定义元字段值以及,任何例子或帮助是非常感谢 这是我的Ajax代码,除了meta-value之外,其他所有字段都可以正常工作,不会在post中添加 jQuery( document ).ready( function ( $ ) { $( '#post-submission-form' ).on( 'submit', function(e) { e.preventDefault(

我刚刚开始使用RESTAPI,并使用它从前端创建帖子。我设法发表了一篇有标题、摘录和内容的文章。 我想添加一个自定义元字段值以及,任何例子或帮助是非常感谢

这是我的Ajax代码,除了meta-value之外,其他所有字段都可以正常工作,不会在post中添加

jQuery( document ).ready( function ( $ ) {
$( '#post-submission-form' ).on( 'submit', function(e) {
    e.preventDefault();
    var title = $( '#post-submission-title' ).val();
    var excerpt = $( '#post-submission-excerpt' ).val();
    var content = $( '#post-submission-content' ).val();
    var status = 'draft';

    var data = {
        title: title,
        excerpt: excerpt,
        content: content,
        status: status,
        meta: {
            'video_url_url' : 'abc',
        }

    };

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

    });

});

})

将此添加到functions.php中:

/**
 * Add the meta fields to REST API responses for posts read and write
 * Read and write a post meta fields in post responses
 */
function mg_register_meta_api() {
    //Meta Fields that should be added to the API 
    $meta_fields = array(
        'video_url_url',
        'another_meta_key'
    );
    //Iterate through all fields and add register each of them to the API
    foreach ($meta_fields as $field) {
        register_rest_field( 'ring',
            $field,
            array(
                'get_callback'    => array( $this, 'mg_fw_get_meta'),
                'update_callback' => array( $this, 'mg_fw_update_meta'),
                'schema'          => null,
            )
        );
    }
}
add_action( 'rest_api_init', 'mg_register_meta_api' );

/**
 * Handler for getting custom field data.
 *
 * @since 0.1.0
 * 
 * @param array $object The object from the response
 * @param string $field_name Name of field
 *
 * @return mixed
 */
function mg_get_meta( $object, $field_name ) {
    return get_post_meta( $object[ 'id' ], $field_name );
}

/**
 * Handler for updating custom field data.
 *
 * @since 0.1.0
 * @link  http://manual.unyson.io/en/latest/helpers/php.html#database
 * @param mixed $value The value of the field
 * @param object $object The object from the response
 * @param string $field_name Name of field
 *
 * @return bool|int
 */
function mg_update_meta( $value, $object, $field_name ) {
    if ( ! $value || ! is_string( $value ) ) {
        return;
    }

    return update_post_meta( $object->ID, $field_name, maybe_serialize( strip_tags( $value ) ) );
}

现在,您应该能够使用api读写元“video\u url\u url”。

我今天正在研究这个问题,并提出了以下解决方案,该解决方案运行良好,并添加了多个字段

希望这有助于任何人谁可能被困在这一点上

    add_action( 'rest_api_init', 'foo_register_meta_api' );

    function foo_register_meta_api() {
        // Meta Fields to add to the API
        $meta_fields = array(
            'field_1',
            'foo_bar',
            'foobar',
            'another_field'
        );
        // Loop all fields and register each of them to the API
        foreach ($meta_fields as $field) {
            register_rest_field( 'my-post-type',
                $field,
                array(
                    'get_callback'    => function ($params) use ($field) {
                        return \get_post_meta($params['id'], $field);
                    },
                    'update_callback' => function ($value, $object, $fieldName){
                        return \update_post_meta($object->ID, $fieldName, $value);
                    },
                    'schema' => null
                )
            );
        }
    }

我正试图在post type
候选者中保存
物理标记
。我得到的是创建帖子,但没有保存
phy\u标记

我的JSON数据:

{ "title": "Why not working finally","content": "Test", "status": "publish" ,"post_type": "candidates", "meta": {
  "phy_marks": 66 } }
我的代码:

add_action( 'rest_api_init', 'foo_register_meta_api' );

function foo_register_meta_api() {
    // Meta Fields to add to the API
    $meta_fields = array(
        'phy_marks'
    );
    // Loop all fields and register each of them to the API
    foreach ($meta_fields as $field) {
        register_rest_field( 'candidates',
            $field,
            array(
                'get_callback'    => function ($params) use ($field) {
                    return \get_post_meta($params['id'], $field);
                },
                'update_callback' => function ($value, $object, $fieldName){
                    return \update_post_meta($object->ID, $fieldName, $value);
                },
                'schema' => null
            )
        );
    }
}

解决了。为了读或写,我必须先注册元字段<代码>$object_type='post'$args1=数组(//验证并清理元值。//“number”必须用作“type”。“type”=>“string”,//在元键的架构中显示。“description”=>“与字符串元值关联的元键”。,//返回该类型的单个值。“single”=>true,//在WP REST API响应中显示。”show_in_rest'=>true,);register_meta($object_type,'video_url_url',$args1);
我也在尝试这样做。我仍然无法让它工作。你能看看我的问题吗: