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

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

问题描述

我是一个完整的 wp 新手,我正在努力通过 WP REST API 获取包含所有元数据(wp_postmeta 表)的帖子.http://v2.wp-api.org/reference/posts/>

非常感谢任何指导.谢谢!

解决方案

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

register_rest_field( 'post', 'metadata', array('get_callback' =>功能($数据){返回 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天全站免登陆