如何在 Wordpress API Post 中包含 META 字段?
How to include META fields in Wordpress API Post?
我使用的是 Wordpress 5.2.1 版,这意味着我没有使用 WP API 的插件。我有工作代码,可以在我的 WP 网站上创建一个新的 post(使用自定义 post 类型),一切都很好。问题是我使用 ACF 创建了自定义字段,但它们没有出现在新创建的 post 中。以下是我通过以下方式发送到 WP 的示例:/wp-json/wp/v2/experience
.
注意:experience
是我自定义的 post 类型。
{'title': 'test', 'content': 'testing from python', 'status': 'draft', 'author': 1, 'meta': {'location': 'NYC', 'date': 'never', 'event_url': 'http://google.com'}, 'featured_media': 1221}
这创建了一个post,但是meta
里面的字段被完全忽略了。我的目标是让我在 meta
中指定的字段包含在我新创建的 post .
中
我已经尝试了以下 URL 中列出的解决方案,但没有任何效果。
https://gist.github.com/rileypaulsen/9b4505cdd0ac88d5ef51
Wordpress Rest API - Custom Fields
https://jeffreyeverhart.com/2017/06/14/adding-custom-fields-wordpress-json-api/
我错过了什么?
@Peter Foti 你能试试下面的函数在你的 API.
中添加元值吗
add_post_meta( <value>, <name>, $meta_value ,true );
参考这个Link
首先,您需要将 'show_in_rest'
属性 设置为 true
并且 'supports
' 属性 应包括 'custom-fields'
当您注册新的 post 类型时。
如果要包含元字段,则需要支持 'custom-fields'。
Note that for meta fields registered on custom post types, the post
type must have custom-fields support. Otherwise, the meta fields will
not appear in the 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
));
}
Note: The meta fields must be unique. Prefix your fields in ACF to
make it unique.
JSON doesn't use single quotes to wrap a string. It uses double
quotes. You are sending invalid JSON.
现在,如果您想创建一个 post 类型的体验。
使用 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)
我使用的是 Wordpress 5.2.1 版,这意味着我没有使用 WP API 的插件。我有工作代码,可以在我的 WP 网站上创建一个新的 post(使用自定义 post 类型),一切都很好。问题是我使用 ACF 创建了自定义字段,但它们没有出现在新创建的 post 中。以下是我通过以下方式发送到 WP 的示例:/wp-json/wp/v2/experience
.
注意:experience
是我自定义的 post 类型。
{'title': 'test', 'content': 'testing from python', 'status': 'draft', 'author': 1, 'meta': {'location': 'NYC', 'date': 'never', 'event_url': 'http://google.com'}, 'featured_media': 1221}
这创建了一个post,但是meta
里面的字段被完全忽略了。我的目标是让我在 meta
中指定的字段包含在我新创建的 post .
我已经尝试了以下 URL 中列出的解决方案,但没有任何效果。
https://gist.github.com/rileypaulsen/9b4505cdd0ac88d5ef51
Wordpress Rest API - Custom Fields
https://jeffreyeverhart.com/2017/06/14/adding-custom-fields-wordpress-json-api/
我错过了什么?
@Peter Foti 你能试试下面的函数在你的 API.
中添加元值吗add_post_meta( <value>, <name>, $meta_value ,true );
参考这个Link
首先,您需要将 'show_in_rest'
属性 设置为 true
并且 'supports
' 属性 应包括 'custom-fields'
当您注册新的 post 类型时。
如果要包含元字段,则需要支持 'custom-fields'。
Note that for meta fields registered on custom post types, the post type must have custom-fields support. Otherwise, the meta fields will not appear in the 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
));
}
Note: The meta fields must be unique. Prefix your fields in ACF to make it unique.
JSON doesn't use single quotes to wrap a string. It uses double quotes. You are sending invalid JSON.
现在,如果您想创建一个 post 类型的体验。 使用 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)