Wordpress Meta未显示在WP REST API查询中

问题描述

我是WP Rest API的新手。只是尝试创建自定义元字段并检索它们。我创建了一个简单的插件,用于在Post Edit屏幕上注册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: 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,$_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&#8217;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);\" title=\"&#8220;Review: Ensemble Fanaa&#8217;s Self-titled Debut Record&#8221; &#8212; 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&#8217;s Self-titled Debut Record</p>\n","Meta": [],<------
    "categories": [
      12
    ],"tags": [],}

我在做什么错?我没有正确注册吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)