| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import apiFetch from '@wordpress/api-fetch'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Update post meta via the REST API |
| 8 |
* |
| 9 |
* @param {number} postId - The post ID to update. |
| 10 |
* @param {Object} metaUpdates - Key-value pairs of meta fields to update. |
| 11 |
* |
| 12 |
* @return {Promise<Object>} - The updated post object. |
| 13 |
*/ |
| 14 |
export async function updatePostMeta( postId, metaUpdates ) { |
| 15 |
const updatedPost = await apiFetch( { |
| 16 |
path: `/wp/v2/posts/${ postId }`, |
| 17 |
method: 'POST', |
| 18 |
data: { |
| 19 |
meta: metaUpdates, |
| 20 |
}, |
| 21 |
} ); |
| 22 |
|
| 23 |
if ( ! updatedPost || ! updatedPost.id ) { |
| 24 |
throw new Error( 'Failed to update post meta.' ); |
| 25 |
} |
| 26 |
|
| 27 |
return updatedPost; |
| 28 |
} |
| 29 |
|