Skip to content

Instantly share code, notes, and snippets.

@mklasen
Last active February 11, 2023 20:31
Show Gist options
  • Save mklasen/b1aa6b9d611fcad9cbe039b09e518453 to your computer and use it in GitHub Desktop.
Save mklasen/b1aa6b9d611fcad9cbe039b09e518453 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
]);
/**
* 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