Opened 7 years ago
Closed 5 years ago
#40408 closed defect (bug) (reported-upstream)
REST API (v2) Does not handle metadata
Reported by: | teamcoltra | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.7.3 |
Component: | REST API | Keywords: | needs-codex |
Focuses: | docs, rest-api | Cc: |
Description
I thought it might have been just my blog so I set up a clean wordpress installation on a fresh VM and even still if my post has metadata in it, it isn't included in the meta section of the REST
https://developer.wordpress.org/rest-api/reference/posts/#schema-meta identifies it as something that should return without any asterisk.
I'm not sure if wordpress.org includes any metadata in their posts but https://wordpress.org/news/wp-json/wp/v2/posts/100 (a random post) doesn't seem to have any either.
Attachments (1)
Change History (11)
#1
follow-up:
↓ 2
@
7 years ago
- Keywords needs-codex added
- Milestone changed from Awaiting Review to WordPress.org
#2
in reply to:
↑ 1
@
7 years ago
Replying to rmccue:
This should be clearer in the documentation, but meta needs to be registered with
show_in_rest => true
set in the `register_meta` call. Seems that we need an additional page in the REST API handbook for this (which I thought we had, but turns out we don't).
This fixed it. I would also suggest a feature request to allow a checkbox or something to just default allow this for all metadata or to allow custom fields to be granted this in the GUI instead of creating a custom plugin for it. That said, that's down the line.
This should be updated in the documentation though. Thanks for your help.
#3
@
7 years ago
There is an example in the documentation - see "Working with Meta & register_meta
" at the bottom of https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/.
Agree that we should make this clearer.
#4
follow-up:
↓ 5
@
7 years ago
it seems to me that the expected process works only with standard posts. In case of custom post types (marked as show_in_rest => true) with proper register_meta (marked as show_in_rest => true as well), it doesnt work if you rely on standard PostsController. In Its constructor it instantiates an WP_REST_Post_Meta_Fields object with proper post_type. However, since method get_meta_type returns "post" instead of proper post_type, get_registered_meta_keys returns wrong values when called by WP_REST_Meta_Fields::get_registered_fields. If WP_REST_Post_Meta_Fields::get_meta_type returned post type as passed in the contructor, everything would have worked...
EDIT: however, if WP_REST_Post_Meta_Fields::get_meta_type is overwritten to return the custom post type it then breaks the get_value method, which relies on get_meta_type for retrieving the actual value with get_metadata( $this->get_meta_type(), $object_id, $meta_key, false );
To me, it seems that using the same method for both fetching the register fields for post type and getting their value is not correct, since values are always retrieved as post_meta whatever the actual type is, while definition of meta is post_type sensitive.
#5
in reply to:
↑ 4
@
7 years ago
As @pissio noted declaring show_in_rest => true
only works as expected on the default post type, but not for custom post types. After playing with the source code it seems like the fix for this is easy and would only require one line to change for the functionality of the default REST controller to work for all post types.
#6
@
7 years ago
Another behavior of the default REST controller that should be documented in the codex is that support for custom-fields
needs to be declared when registering a custom post type or else metafields registered to show in rest will not show up in the result at all.
This ticket was mentioned in Slack in #core-restapi by sgarza. View the logs.
7 years ago
This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.
7 years ago
This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.
5 years ago
#10
@
5 years ago
- Milestone WordPress.org deleted
- Resolution set to reported-upstream
- Status changed from new to closed
The docs now mention needing to use show_in_rest
when registering meta. But they don't yet mention the need to add custom-fields
support.
I've created a Github issue to track this in the WP REST API docs repo: https://github.com/WP-API/docs/issues/51
@sgarza you can now use register_post_meta
to register metadata for a specific post type: https://make.wordpress.org/core/2018/07/27/registering-metadata-in-4-9-8/
This should be clearer in the documentation, but meta needs to be registered with
show_in_rest => true
set in the `register_meta` call. Seems that we need an additional page in the REST API handbook for this (which I thought we had, but turns out we don't).