#55365 closed defect (bug) (worksforme)
Register meta field to attachment, but use media in REST API
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | REST API | Keywords: | |
Focuses: | javascript, docs, rest-api | Cc: |
Description
Hi there,
I found either a bug or a documentation issue.
The codex https://developer.wordpress.org/rest-api/reference/media/ refers to a meta
that is both editable and readable.
I am writing a Gutenberg block and wanted to store data in the meta
field. I am using:
Code highlighting:
apiFetch( { path: '/wp/v2/media/' + mediaId, method: 'POST', data: { title: title, description: description, meta: { some_data: "some_value" } } } )
However, it always returns an empty array as meta
I also tried setting some custom field
Code highlighting:
register_meta( 'media', 'some_data', array( 'type' => 'string', 'description' => 'Some data', 'single' => true, 'show_in_rest' => true ) );
I eventually tried format the meta
field I am sending
Code highlighting:
meta: [{ some_data: "some_value" }]
or
Code highlighting:
meta: [{ key: "some_data", value: "some_value" }]
But still nothing.
Do you have any idea?
Change History (3)
#2
@
15 months ago
- Keywords close added
- Resolution set to worksforme
- Status changed from new to closed
- Summary changed from meta field in media via REST API to Register meta field to attachment, but use media in REST API
Hi @millerf01, welcome back and thank you for the ticket!
You were almost there, the apiFetch(...)
request is correct, only the meta
registration needs some polish. Need to use attachment
instead of media
:
There are two options for that:
<?php register_meta( 'post', 'some_data', array( 'object_subtype' => 'attachment', 'type' => 'string', 'description' => 'Some data', 'single' => true, 'show_in_rest' => true ) );
or
<?php register_post_meta( 'attachment', 'some_data', array( 'type' => 'string', 'description' => 'Some data', 'single' => true, 'show_in_rest' => true ) );
Sorry, here is the image I was supposed to have linked up there