WordPress.org

Make WordPress Core

Opened 14 months ago

Last modified 8 months ago

#40408 new defect (bug)

REST API (v2) Does not handle metadata

Reported by: teamcoltra Owned by:
Milestone: WordPress.org 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)

40408_1.patch (627 bytes) - added by sgarza 8 months ago.
Proposed bug fix for showing metafields for custom post types in the REST API

Download all attachments as: .zip

Change History (9)

#1 follow-up: @rmccue
14 months ago

  • Keywords needs-codex added
  • Milestone changed from Awaiting Review to WordPress.org

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).

#2 in reply to: ↑ 1 @teamcoltra
14 months 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 @jnylen0
14 months 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: @pissio
13 months 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.

Last edited 13 months ago by pissio (previous) (diff)

#5 in reply to: ↑ 4 @sgarza
8 months 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.

@sgarza
8 months ago

Proposed bug fix for showing metafields for custom post types in the REST API

#6 @sgarza
8 months 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.


8 months ago

This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.


8 months ago

Note: See TracTickets for help on using tickets.