rest api – Editor API Endpoint or Data Store That Provides a List of Meta Fields for Post Types

Question

I’m building a custom block that allows an editor to choose a Meta Key from a dropdown menu:

enter image description here

However, I am unable to find a data store or API endpoint available (out of the box) that will provide a list of meta keys available for a particular post type — or even just a list of all registered meta keys.

I could, of course, create my own endpoint to output this data — and that may be the best route and will likely be my next approach — however, I wanted to make sure there wasn’t a native data store or API endpoint that I am missing.

Here is what I have tried:

/wp-json/wp/v2/types/MY_CUSTOM_POST_TYPE?context=edit&_wpnonce=NONCE

This is very close as it provides a plethora of information about the registered post type, but it does not provide information about meta fields registered with this associated endpoint.

Similarly, I have tried:

wp.data.select('core').getPostType('MY_CUSTOM_POST_TYPE'), which again, while close, does not provide registered meta fields.

To confirm, I have registered the meta data with my post type and this data appears correctly when using the API to query a collection or single “ordinance” (the name of my custom post type in this case).

/**
 * Registers Ordinance Meta Fields.
 */
function pan_ordinance_register_meta() {
    register_post_meta(
        'ordinance',
        'bill_author',
        [
            'type'         => 'string',
            'description'  => __( 'Bill Author', 'pan' ),
            'single'       => true,
            'show_in_rest' => true,
        ]
    );
    register_post_meta(
        'ordinance',
        'bill_location',
        [
            'type'         => 'string',
            'description'  => __( 'Bill Location', 'pan' ),
            'single'       => true,
            'show_in_rest' => true,
        ]
    );
    ...
);

0
Phil Birnie 1 year 2022-01-10T13:49:05-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse