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 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!
You must be logged in to post a comment.
I found an easy solution for this. In the current theme – functions.php add the following code:
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”
if you want single field use:
don’t forget change hs_views field to what you want
if you want all fields use:
check reference: get_post_meta
Whilst register_rest_route is good for extensive customization, WordPress allows you to expose custom meta fields in the default WordPress REST API, without having to build your own endpoints.
You can use register_meta to expose certain WordPress Meta fields for certain post types. By default, meta fields aren’t registered to be exposed to the Rest API.
In this example, we set default ‘posts’ to show the meta field with the key “type” in the rest api. Specifically, show_in_rest set to true achieves this.
Read More -> https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/#using-register_rest_field-vs-register_meta