2017-04-04 37 views
1

我剛剛開始使用REST API並使用它從前端創建帖子。我設法發佈標題,摘錄,內容的帖子。 我想添加一個自定義元字段值,以及任何示例或幫助,非常感謝。使用Meta字段創建帖子 - WP REST API

這是我的Ajax代碼,是不是在後期添加的所有其他領域除了meta值做工精細

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

    }); 

}); 

});

+0

解決了它。我必須先註冊meta字段才能讀取或寫入。 '$ object_type ='post'; $ args1 = array(//驗證並清理元值 //'number'必須用作'type' 'type'=>'string', //顯示元鍵的模式 'description'=>'一個與字符串元值相關聯的元密鑰', //返回一個單一的值類型 'single'=> true, //在WP REST API響應中顯示。 'show_in_rest'=> true, ); register_meta($ object_type,'video_url_url',$ args1);' –

+0

我試圖做同樣的事情。我仍然無法工作。你可以看看我的問題:http://stackoverflow.com/q/43265580/100747 – input

回答

0

添加到您的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_url_url」。