WP-API RESTFULL JSON和POST META?

时间:2014-07-16 作者:vimes1984

好的,我正在使用这个插件:https://github.com/WP-API/WP-API/ 将我的站点转换为rest完整API,效果很好。。然后我使用ths插件添加自定义帖子元:https://wordpress.org/plugins/types/我的问题是,我似乎无法查询url端点以返回我的帖子元,就像我在查询以下内容一样:

get_post_meta( $post_id, $key = \'\', $single = false )
我该怎么办?克里斯

2 个回复
最合适的回答,由SO网友:brianlmerritt 整理而成

自定义帖子忽略了普通过滤器-看看class-wp-json-pages.php 了解如何为自定义帖子构建一个。

<?php
/**
 * Page post type handlers
 *
 * @package WordPress
 * @subpackage JSON API
 */

/**
 * Page post type handlers
 *
 * This class serves as a small addition on top of the basic post handlers to
 * add small functionality on top of the existing API.
 *
 * In addition, this class serves as a sample implementation of building on top
 * of the existing APIs for custom post types.
 *
 * @package WordPress
 * @subpackage JSON API
 */
class WP_JSON_Pages extends WP_JSON_CustomPostType {
    /**
     * Base route
     *
     * @var string
     */
    protected $base = \'/pages\';

    /**
     * Post type
     *
     * @var string
     */
    protected $type = \'page\';

    /**
     * Register the page-related routes
     *
     * @param array $routes Existing routes
     * @return array Modified routes
     */
    public function register_routes( $routes ) {
        $routes = parent::register_routes( $routes );
        $routes = parent::register_revision_routes( $routes );
        $routes = parent::register_comment_routes( $routes );

        // Add post-by-path routes
        $routes[ $this->base . \'/(?P<path>.+)\'] = array(
            array( array( $this, \'get_post_by_path\' ),    WP_JSON_Server::READABLE ),
            array( array( $this, \'edit_post_by_path\' ),   WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
            array( array( $this, \'delete_post_by_path\' ), WP_JSON_Server::DELETABLE ),
        );

        return $routes;
    }

    /**
     * Retrieve a page by path name
     *
     * @param string $path
     * @param string $context
     *
     * @return array|WP_Error
     */
    public function get_post_by_path( $path, $context = \'view\' ) {
        $post = get_page_by_path( $path, ARRAY_A );

        if ( empty( $post ) ) {
            return new WP_Error( \'json_post_invalid_id\', __( \'Invalid post ID.\' ), array( \'status\' => 404 ) );
        }

        return $this->get_post( $post[\'ID\'], $context );
    }

    /**
     * Edit a page by path name
     *
     * @param $path
     * @param $data
     * @param array $_headers
     *
     * @return true|WP_Error
     */
    public function edit_post_by_path( $path, $data, $_headers = array() ) {
        $post = get_page_by_path( $path, ARRAY_A );

        if ( empty( $post ) ) {
            return new WP_Error( \'json_post_invalid_id\', __( \'Invalid post ID.\' ), array( \'status\' => 404 ) );
        }

        return $this->edit_post( $post[\'ID\'], $data, $_headers );
    }

    /**
     * Delete a page by path name
     *
     * @param $path
     * @param bool $force
     *
     * @return true|WP_Error
     */
    public function delete_post_by_path( $path, $force = false ) {
        $post = get_page_by_path( $path, ARRAY_A );

        if ( empty( $post ) ) {
            return new WP_Error( \'json_post_invalid_id\', __( \'Invalid post ID.\' ), array( \'status\' => 404 ) );
        }

        return $this->delete_post( $post[\'ID\'], $force );
    }

    /**
     * Prepare post data
     *
     * @param array $post The unprepared post data
     * @param string $context The context for the prepared post. (view|view-revision|edit|embed|single-parent)
     * @return array The prepared post data
     */
    protected function prepare_post( $post, $context = \'view\' ) {
        $_post = parent::prepare_post( $post, $context );

        // Override entity meta keys with the correct links
        $_post[\'meta\'][\'links\'][\'self\'] = json_url( $this->base . \'/\' . get_page_uri( $post[\'ID\'] ) );

        if ( ! empty( $post[\'post_parent\'] ) ) {
            $_post[\'meta\'][\'links\'][\'up\'] = json_url( $this->base . \'/\' . get_page_uri( (int) $post[\'post_parent\'] ) );
        }

        return apply_filters( \'json_prepare_page\', $_post, $post, $context );
    }
}
将“页面”替换为“MyCustomPostTypes”,将页面替换为“mycustomposttype”。请注意不要重命名也使用术语页的内部WordPress代码

注意:最好将其作为插件添加,而不是更改JSON-WP-API插件

/**
 * Plugin Name: MyCustom JSON App API
 * Description: MyCustomPost handler for the JSON API
 * Dependency:  This plugin requires JSON-WP-API Plugin!!!! 
 * Author: 
 * Author URI: 
 * Version: 
 * Plugin URI: 
 */

SO网友:user3159159

如果知道元字段的键,则可以使用此筛选器:

add_filter( \'json_prepare_post\', function ( $data, $post, $context) {
    $data[\'myextradata\'] = array(
        \'somekey\' => get_post_meta( $post[\'ID\'], \'abcdef\', true ),
    );
    return $data;
}, 10, 3 );
更换abcdef 使用元字段的键。然后,如果您得到一个posts的json,那么abcdef 将加入myextradata 指数

我不熟悉“Types”插件,但快速查看一下WordPress MySQL数据库,就会发现“Types”用于其元字段的键的名称。

资料来源:https://wordpress.org/support/topic/custom-meta-data-2

结束

相关推荐