function action_rest_insert_post( $post, $request, $true ) {
$params = $request->get_json_params();
if(array_key_exists("terms", $params)) {
foreach($params["terms"] as $taxonomy => $terms) {
wp_set_post_terms($post->ID, $terms, $taxonomy);
}
}
}
add_action("rest_insert_post", "action_rest_insert_post", 10, 3);
I used the post, but it should not be otherwise for custom post types, just change the action it enters. This works fine for me on a call with something like this:
{
"title": "The story of Dr Foo",
"content": "This is the story content",
"status":"publish",
"excerpt":"Dr Foo story",
"terms": {
"mytaxonomy": [ "myterm", "anotherterm" ]
}
}
If you want to send this data via POST, you will need to change the way you receive the parameters
$params = $request->get_body_params();
and send the data as:
title=testtitle&content=testcotnent&status=publish&excerpt=testexcert&terms[mytaxonomy][]=myterm&terms[mytaxonomy][]=anotherterm
source
share