{"id":1183,"date":"2022-07-24T14:27:37","date_gmt":"2022-07-24T14:27:37","guid":{"rendered":"https:\/\/mklasen.com\/?p=1183"},"modified":"2022-07-24T14:36:05","modified_gmt":"2022-07-24T14:36:05","slug":"updating-custom-post-meta-via-the-wordpress-rest-api","status":"publish","type":"post","link":"https:\/\/mklasen.com\/updating-custom-post-meta-via-the-wordpress-rest-api\/","title":{"rendered":"Updating custom post meta via the WordPress REST API"},"content":{"rendered":"\n
When working with the WordPress REST API we sometimes have to update custom post meta for a (custom post type) post. In my case, I am using the available REST endpoints but I did want to add some extra data to the GET request, and wanted to make it possible to update that data as well.<\/p>\n\n\n\n
It’s actually quite easy to get this to work in WordPress, we’ll hook into the In the example above we’re extending the session post type with a “start_time” field. The “start_time” field is saved as post meta with the We can then use something like a fetch request to get and update these fields like in the example below:<\/p>\n\n\n\n Note: Consider these examples as starting points. In my case, they’re used for a PoC and security has not been considered in yet as that also makes the code more difficult to understand.<\/p>\n\n\n\n Happy developing!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":" When working with the WordPress REST API we sometimes have to update custom post meta for a (custom post type) post. In my case, I am using the available REST endpoints but I did want to add some extra data to the GET request, and wanted to make it possible to update that data as […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[1],"tags":[],"acf":[],"yoast_head":"\nrest_api_init<\/code> and use the
register_rest_field<\/code> function to add this features, like in the example below:<\/p>\n\n\n\n
add_action( 'rest_api_init', array( $this, 'add_fields' ) );<\/code><\/pre>\n\n\n\n
public function add_fields() {\n\tregister_rest_field(\n\t\t'session',\n\t\t'start_time',\n\t\tarray(\n\t\t\t'get_callback' => function() {\n\t\t\t\treturn get_post_meta( get_the_ID(), '_sympose_session_start', true );\n\t\t\t},\n\t\t\t'update_callback' => function( $value, $object, $field_name ) {\n\t\t\t\treturn update_post_meta($object->ID, '_sympose_session_start', $value );\n\t\t\t},\n\t\t)\n\t);\n}<\/code><\/pre>\n\n\n\n
_sympose_session_start<\/code> key. Change these variables to your own and you’re good to go!<\/p>\n\n\n\n
let post_id = 10;\nlet start_time_value = '10:00';\n\nfetch(wpApiSettings.root + \"wp\/v2\/session\/\" + post_id, {\n\tmethod: \"POST\",\n\theaders: {\n\t\t\"X-WP-Nonce\": wpApiSettings.nonce,\n\t\t'Content-Type': 'application\/json',\n\t},\n\tbody: JSON.stringify({\n\t\tstart_time: start_time_value,\n\t}),\n})\n.then(res => res.json())\n.then(result => {\n\tconsole.log(result);\n});<\/code><\/pre>\n\n\n\n