I'm using REST API, but I have a taxonomy called Ingredients to my Product custom post type.
I can create a Product posting to the endpoing /wp-json/wp/v2/produtos
, but how can I attach a custom taxonomy (Ingredient) to it?
When you create (or update) a post via the REST API, you can use the taxonomy slug (or the taxonomy's custom rest_base
, if any) to assign terms in that taxonomy to the post that you are creating/updating. I.e. In the request body (or POST data), use the slug as a parameter in the request and set its value to the ID of the term you'd like to assign to the post. But you can also supply a list of IDs (e.g. my_tax=123,456
or my_tax[]=123&my_tax[]=456
).
So in your case, assuming the slug (and rest_base
) is ingredient
, here's an example using cURL and the Application Passwords plugin:
curl --user "<WORDPRESS USERNAME>:<APPLICATION PASSWORD>" -X POST^
-d "title=Test+1&content=Foo+bar&ingredient=123,456"^
https://example.com/wp-json/wp/v2/produtos
curl --user "<WORDPRESS USERNAME>:<APPLICATION PASSWORD>" -X POST^
-d "title=Test+2&content=Baz+boo&ingredient[]=123&ingredient[]=7"^
https://example.com/wp-json/wp/v2/produtos
And as a reminder, the ingredient
or taxonomy parameter accepts only a number or a list of numbers. But there are filters like rest_after_insert_<post type>
filter if you want to use your own custom parameter and for example, allows using term slug like ingredient_slugs=my-term
.
Also, the user associated in the request must have the permission to assign terms to posts.