wp rest api获取带有其元数据的帖子 [英] wp rest api get posts with their meta

查看:94
本文介绍了wp rest api获取带有其元数据的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个wp newb,我正努力通过WP REST API来获取包含其所有meta(wp_postmeta表)的帖子. http://v2.wp-api.org/reference/posts/

任何指导都将不胜感激.谢谢!

解决方案

我找到了一个简单的解决方案.在当前主题-functions.php中添加以下代码:

register_rest_field( 'post', 'metadata', array(
    'get_callback' => function ( $data ) {
        return get_post_meta( $data['id'], '', '' );
    }, ));

它将返回帖子/包含所有元数据的帖子. IE. http://localhost/rest_api/wp-json/wp/v2/posts 或者 http://localhost/rest_api/wp-json/wp/v2/post/58

发布后的元数据将位于元数据"中

I am a total wp newb and I am struggling to get posts with all their meta(wp_postmeta table) via the WP REST API. http://v2.wp-api.org/reference/posts/

Any guidance is greatly appreciated. Thanks!

解决方案

I found an easy solution for this. In the current theme - functions.php add the following code:

register_rest_field( 'post', 'metadata', array(
    'get_callback' => function ( $data ) {
        return get_post_meta( $data['id'], '', '' );
    }, ));

It will return posts / post with all it's meta. I.e. http://localhost/rest_api/wp-json/wp/v2/posts or http://localhost/rest_api/wp-json/wp/v2/post/58

post meta will be in "metadata"

这篇关于wp rest api获取带有其元数据的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆