问题描述
我是一个完整的 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/postsorhttp://localhost/rest_api/wp-json/wp/v2/post/58
post meta will be in "metadata"
这篇关于wp rest api 获取带有元数据的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!