Skip to content

Instantly share code, notes, and snippets.

@tolawho
Forked from mklasen/extend-rest-api.php
Created October 19, 2020 16:34
Show Gist options
  • Save tolawho/f38970de0e1592c6e5782b7e44575567 to your computer and use it in GitHub Desktop.
Save tolawho/f38970de0e1592c6e5782b7e44575567 to your computer and use it in GitHub Desktop.
Update Custom Post Type Meta Fields with the WordPress REST API
<?php
register_meta('post', 'custom-field', [
'object_subtype' => 'custom-post-type',
'show_in_rest' => true
]);
/**
* https://stroopwafels.test/wp-json/wp/v2/design-template/
* Make sure to have something like https://github.com/WP-API/Basic-Auth active on your site
* Make the request with authentication (Basic Auth and username+password when using the link above)
*/
Send as body data (JSON):
{
"title": "Test title",
"meta": {
"custom-field": "test"
}
}
<?php
register_post_type( 'custom-post-type', array(
'label' => __( 'Custom Post Type', 'textdomain' ),
'show_ui' => true,
'menu_icon' => 'dashicons-admin-customizer',
'label' => 'Custom Post Type'
'supports' => array( 'title', 'thumbnail', 'page-attributes', 'custom-fields' ),
'public' => false,
'rewrite' => false,
'show_in_rest' => true,
) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment