Skip to content

Instantly share code, notes, and snippets.

@KustomDeveloper
Created December 30, 2021 21:17
Show Gist options
  • Save KustomDeveloper/83b63a7435f227661fe8c3378499c8f5 to your computer and use it in GitHub Desktop.
Save KustomDeveloper/83b63a7435f227661fe8c3378499c8f5 to your computer and use it in GitHub Desktop.
Register custom meta box on custom post type to show in wp rest api
/*
* Register custom meta box on custom post type to show in wp rest api
*
* Custom post type must support 'custom-fields' and have the 'show_in_rest' parameter set to true
*/
//Custom post type is 'audio' and custom meta box is 'cloud_url'
add_action( 'rest_api_init', function () {
register_rest_field( 'audio', 'cloud_url', array(
'get_callback' => function( $post_arr ) {
return get_post_meta( $post_arr['id'], 'cloud_url', true );
},
) );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment