Wordpress Meta未显示在WP REST API查询中
我是WPRESTAPI的新手。只是尝试创建自定义元字段并检索它们。我创建了一个简单的插件,在编辑后的屏幕上注册meta值和metabox,效果很好Wordpress Meta未显示在WP REST API查询中,wordpress,custom-fields,wordpress-rest-api,Wordpress,Custom Fields,Wordpress Rest Api,我是WPRESTAPI的新手。只是尝试创建自定义元字段并检索它们。我创建了一个简单的插件,在编辑后的屏幕上注册meta值和metabox,效果很好 <?php /** * @package api_testing * @version 1.0.0 */ /* Plugin Name: API Testing Plugin URI: http://example.com/ Description: The beginning of an awesome plugin Author: M
<?php
/**
* @package api_testing
* @version 1.0.0
*/
/*
Plugin Name: API Testing
Plugin URI: http://example.com/
Description: The beginning of an awesome plugin
Author: Me
Version: 1.0.0
Author URI: http://example.com/
*/
function generate_press_type() {
$labels = array(
'name' => 'Press Articles',
'singular_name' => 'Press',
);
$args = array(
'label' => 'Press',
'labels' => $labels,
'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail' ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'rest_base' => 'press-articles',
'rewrite' => true,
'rewrite_slug' => '',
);
register_post_type( 'press_article', $args );
}
add_action( 'init', 'generate_press_type', 0 );
function create_meta() {
$args1 = array(
'type' => 'string', // Validate and sanitize the meta value as a string.
// In 4.7 one of 'string', 'boolean', 'integer', 'number' must be used as 'type'.
'description' => 'A meta key associated with a string meta value.', // Shown in the schema for the meta key.
'single' => true, // Return a single value of the type. Default: false.
'show_in_rest' => true, // Show in the WP REST API response. Default: false.
);
register_meta( 'post', 'wporg_field', $args1 );
}
function wporg_add_custom_box() {
$screens = [ 'post', 'wporg_cpt' ];
foreach ( $screens as $screen ) {
add_meta_box(
'wporg_box_id', // Unique ID
'Custom Meta Box Title', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action( 'add_meta_boxes', 'wporg_add_custom_box' );
function wporg_custom_box_html( $post ) {
$value = get_post_meta( $post->ID, '_wporg_meta_key', true );
?>
<label for="wporg_field">Description for this field</label>
<select name="wporg_field" id="wporg_field" class="postbox">
<option value="">Select something...</option>
<option value="something" <?php selected( $value, 'something' ); ?>>Something</option>
<option value="else" <?php selected( $value, 'else' ); ?>>Else</option>
</select>
<?php
}
function wporg_save_postdata( $post_id ) {
if ( array_key_exists( 'wporg_field', $_POST ) ) {
update_post_meta(
$post_id,
'_wporg_meta_key',
$_POST['wporg_field']
);
}
}
add_action( 'save_post', 'wporg_save_postdata' );
此字段的说明
选择一些。。。
>否则
{
"id": 1172,
"date": "2019-09-20T17:50:23",
"date_gmt": "2019-09-21T00:50:23",
"guid": {
"rendered": "http://example.com/?p=1172"
},
"modified": "2020-11-08T08:56:43",
"slug": "review-ensemble-fanaas-self-titled-debut-record",
"status": "publish",
"type": "post",
"link": "http://example.com/review-ensemble-fanaas-self-titled-debut-record/",
"title": {
"rendered": "Review: Ensemble Fanaa’s Self-titled Debut Record"
},
"content": {
"rendered": "<blockquote class=\"wp-embedded-content\" data-secret=\"LyNp7sCidr\"><p><a href=\"https://www.jazzrightnow.com/review-ensemble-fanaas-self-titled-debut-record/\">Review: Ensemble Fanaa’s Self-titled Debut Record</a></p></blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"“Review: Ensemble Fanaa’s Self-titled Debut Record” — Jazz Right Now\" src=\"https://www.jazzrightnow.com/review-ensemble-fanaas-self-titled-debut-record/embed/#?secret=LyNp7sCidr\" data-secret=\"LyNp7sCidr\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"></iframe></p>\n",
"protected": false
},
"excerpt": {
"rendered": "<p>Review: Ensemble Fanaa’s Self-titled Debut Record</p>\n",
"protected": false
},
"meta": [], <------
"categories": [
12
],
"tags": [],
}