Make WordPress Core

Opened 21 months ago

Closed 15 months ago

Last modified 15 months ago

#55365 closed defect (bug) (worksforme)

Register meta field to attachment, but use media in REST API

Reported by: millerf01's profile millerf01 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

https://imgur.com/a/8XcUJ5b

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)

#1 @millerf01
21 months ago

Sorry, here is the image I was supposed to have linked up there

https://i.imgur.com/RyO4eVo.png

#2 @anna.bansaghi
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
) );
Last edited 15 months ago by anna.bansaghi (previous) (diff)

#3 @desrosj
15 months ago

  • Keywords close removed
  • Milestone Awaiting Review deleted

@millerf01 Just wanted to reach out to confirm that @annabansaghi's suggestion fixes your specific issue.

Note: See TracTickets for help on using tickets.