#39803 closed defect (bug) (duplicate)
REST API meta type not working properly for custom post types
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | has-patch dev-feedback |
Focuses: | rest-api | Cc: |
Description
When attempting to set meta via a custom post types api endpoint, the meta type is always set to post
. This results in all meta updates for custom post types via the REST API failing.
For example:
$args = [
'type' => 'string',
'single' => true,
'show_in_rest' => true
];
register_meta( 'book', 'author', $args );
If I'm understanding correctly, the above should expose the 'author' meta to the REST API. However, in the REST API, all of these fail due to a check against WP_REST_Post_Meta_Fields->get_registered_fields()
.
WP_REST_Post_Meta_Fields->get_registered_fields()
in turn matches meta keys to get_registered_meta_keys( $this->get_meta_type() )
which always returns post
.
Not sure if any of that makes sense...
The fix would be to make WP_REST_Post_Meta_Fields->get_meta_type()
return the post type as far as I can tell.
I would love to tackle this one, however I wanted to get some input from someone more well-versed in the REST API codebase before tackling this to be sure that there isn't a reason that WP_REST_Post_Meta_Fields->get_meta_type()
is explicitly set to return post
.
Attachments (1)
Change History (6)
#3
follow-up:
↓ 5
@
8 years ago
- Milestone Awaiting Review deleted
- Resolution set to duplicate
- Status changed from new to closed
@chriseverson Thank you for opening this ticket, and thank you @rheinardkorf for clarifying that this is a deficiency of register_meta
. However, the issue that @chriseverson reported is separate from the custom-fields
issue; it is true that registered meta cannot be restricted to a particular custom post type, which is a major weakness of the API's current meta capabilities.
I'm going to close this as a duplicate of #38323, which is the ticket tracking the effort to provide a new argument to register_meta
indicating that a particular meta value is intended only for use with a specific custom post type. I would welcome your participation in that thread!
(And if I misunderstood your issue, please note as much below and we can continue the conversation.)
#4
in reply to:
↑ 2
@
8 years ago
Replying to rheinardkorf:
In the docs (here: https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/#working-with-meta-and-register_meta) you will see in the comments that for custom post types the $object_type has to be "post".
Looking back through the documentation for register_meta()
, I see that I incorrectly passed the post type instead of the object type, this is where I ran into issues. That said, it doesn't seem to be exactly the functionality that I'm looking for as I'd like the meta fields to be post type specific.
#5
in reply to:
↑ 3
@
8 years ago
Replying to kadamwhite:
@chriseverson Thank you for opening this ticket, and thank you @rheinardkorf for clarifying that this is a deficiency of
register_meta
. However, the issue that @chriseverson reported is separate from thecustom-fields
issue; it is true that registered meta cannot be restricted to a particular custom post type, which is a major weakness of the API's current meta capabilities.
I'm going to close this as a duplicate of #38323, which is the ticket tracking the effort to provide a new argument to
register_meta
indicating that a particular meta value is intended only for use with a specific custom post type. I would welcome your participation in that thread!
(And if I misunderstood your issue, please note as much below and we can continue the conversation.)
Most excellent, thank you!
@chriseverson I had this problem a couple of days ago. The patch will not resolve the issue.
In the docs (here: https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/#working-with-meta-and-register_meta) you will see in the comments that for custom post types the $object_type has to be "post".
What was not documented until recently is that for meta to work is that you need to enable
custom-fields
support for your post type. This can be done by adding it to the "supports" array of your post type or by usingadd_theme_support()
. See this in the docs:It had me tearing my hair out, but the answer ended up being simple.