在获取的JSON中包含自定义POST元值

时间:2017-04-13 作者:Toby

我有一个自定义的帖子类型note 我可以使用WP REST API轻松获取帖子:

http://example.com/wp-json/wp/v2/note

工作正常!但在JSON字符串中,我希望它包含名为hello_world. 例如:

"hello_world": "value...",

如何使JSON包含此自定义post meta?

1 个回复
SO网友:hwl

要看两件事:1)register_meta() 2) 自定义字段支持post类型。

1) register\\u meta您可以使用show_in_rest => true 使其可通过api访问。本页底部:Modifying Responses, 提供以下示例:

<?php
// The object type. For custom post types, this is \'post\';
// for custom comment types, this is \'comment\'. For user meta,
// this is \'user\'.
$object_type = \'post\';
$args1 = array( // Validate and sanitize the meta value.
    // Note: currently (4.7) one of \'string\', \'boolean\', \'integer\',
    // \'number\' must be used as \'type\'. The default is \'string\'.
    \'type\'         => \'string\',
    // Shown in the schema for the meta key.
    \'description\'  => \'A meta key associated with a string meta value.\',
    // Return a single value of the type.
    \'single\'       => true,
    // Show in the WP REST API response. Default: false.
    \'show_in_rest\' => true,
);
register_meta( $object_type, \'my_meta_key\', $args1 );
自定义字段支持上面的页面提到您的CPT需要custom-fields 支持将其显示在响应中。

添加custom-fields 至支架阵列\'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'custom-fields\') 注册post类型时,在$args数组中。

在CPT注册的上下文中,我将其添加为$args数组最后一行的最后一项。

add_action( \'init\', \'codex_book_init\' );
/**
 * Register a book post type.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
function codex_book_init() {
    $labels = array(
        \'name\'               => _x( \'Books\', \'post type general name\', \'your-plugin-textdomain\' ),
        \'singular_name\'      => _x( \'Book\', \'post type singular name\', \'your-plugin-textdomain\' ),
        \'menu_name\'          => _x( \'Books\', \'admin menu\', \'your-plugin-textdomain\' ),
        \'name_admin_bar\'     => _x( \'Book\', \'add new on admin bar\', \'your-plugin-textdomain\' ),
        \'add_new\'            => _x( \'Add New\', \'book\', \'your-plugin-textdomain\' ),
        \'add_new_item\'       => __( \'Add New Book\', \'your-plugin-textdomain\' ),
        \'new_item\'           => __( \'New Book\', \'your-plugin-textdomain\' ),
        \'edit_item\'          => __( \'Edit Book\', \'your-plugin-textdomain\' ),
        \'view_item\'          => __( \'View Book\', \'your-plugin-textdomain\' ),
        \'all_items\'          => __( \'All Books\', \'your-plugin-textdomain\' ),
        \'search_items\'       => __( \'Search Books\', \'your-plugin-textdomain\' ),
        \'parent_item_colon\'  => __( \'Parent Books:\', \'your-plugin-textdomain\' ),
        \'not_found\'          => __( \'No books found.\', \'your-plugin-textdomain\' ),
        \'not_found_in_trash\' => __( \'No books found in Trash.\', \'your-plugin-textdomain\' )
    );

    $args = array(
        \'labels\'             => $labels,
        \'description\'        => __( \'Description.\', \'your-plugin-textdomain\' ),
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'book\' ),
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => array( 
                                     \'title\', 
                                     \'editor\', 
                                     \'author\', 
                                     \'thumbnail\', 
                                     \'excerpt\', 
                                     \'comments\',
                                     \'custom-fields\' 
                                     )
        );

    register_post_type( \'book\', $args );
}