首先,您需要将'show_in_rest' 属性设置为true,并且当您注册新的帖子类型时,“supports”属性应包含'custom-fields'。
如果要包含元字段,则需要支持“自定义字段”。
请注意,对于在自定义帖子类型上注册的元字段,帖子
类型必须支持自定义字段。否则,元字段将
不会出现在 REST API 中。 https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
function cptui_register_my_cpts() {
/**
* Post Type: Experiences.
*/
$labels = array(
"name" => __( "Experiences", "twentynineteen" ),
"singular_name" => __( "Experience", "twentynineteen" ),
);
$args = array(
"label" => __( "Experiences", "twentynineteen" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "experience", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "custom-fields" ),
);
register_post_type( "experience", $args );
}
add_action( 'init', 'cptui_register_my_cpts' );
现在,您需要使用register_meta() 注册元字段。
add_action( 'rest_api_init', 'register_experience_meta_fields');
function register_experience_meta_fields(){
register_meta( 'post', 'location', array(
'type' => 'string',
'description' => 'event location',
'single' => true,
'show_in_rest' => true
));
register_meta( 'post', 'date', array(
'type' => 'string',
'description' => 'event location',
'single' => true,
'show_in_rest' => true
));
register_meta( 'post', 'event_url', array(
'type' => 'string',
'description' => 'event location',
'single' => true,
'show_in_rest' => true
));
}
注意:元字段必须是唯一的。将 ACF 中的字段前缀为
让它独一无二。
JSON 不使用单引号来包装字符串。它使用双
引号。您发送的 JSON 无效。
现在,如果您想创建体验帖子类型。
使用 JSON linter 验证您的 json。 https://jsonlint.com/
使用字段向http://paathsala-plugin.test/wp-json/wp/v2/experience 发出 POST 请求
{
"title": "test",
"content": "testingfrompython",
"status": "draft",
"author": 1,
"meta": {
"location": "NYC",
"date": "never",
"event_url": "http: //google.com"
},
"featured_media": 1221
}
WordPress 不允许您直接创建资源。您需要验证您的 REST 请求。我正在使用 Basic Auth 来验证 WordPress REST API。你需要安装一个插件。从这里获取它:https://github.com/WP-API/Basic-Auth
我已经测试了以下python代码。
import base64
import json
import requests;
# Data to be send
data = {
"title": "test",
"content": "testingfrompython",
"status": "draft",
"author": 1,
"meta": {
"location": "NYC",
"date": "never",
"event_url": "http: //google.com"
},
"featured_media": 1221
}
# I am using basic auth plugin to for WP API authenticaiton
username = 'admin'
password = 'blood9807'
# Encode the username and password using base64
creds = base64.b64encode(username + ':' + password)
# Create headers to be send
headers = {
'Authorization': 'Basic ' + creds,
'Content-type': 'application/json',
'Accept': 'text/plain'
}
# Convert the python dictionary to JSON
data_json = json.dumps(data)
# Create a post
r = requests.post('http://paathsala-plugin.test/wp-json/wp/v2/experience', data = data_json, headers = headers )
print(r)