Are you sure you can’t use wp_set_object_terms() in REST? I feel like you should be able to, have you tried?
@howdy_mcgee At the website that I work on it, there is a theme that defines and register all taxonomies. Well I don’t load this theme for REST API requests, so I can not access to taxonomies (If I want to use WordPress Terms functions, I will receive some errors). I tried to find how and where this theme registers taxonomies, but I didn’t find. So, I think it is easier to write custom query for assigning tags or categories to custom posts.
Are you specifically getting errors that wp_set_object_terms() function does not exist in your plugin? Can you post the REST code you have for us to try?
@howdy_mcgee
Thanks for you reply.
I explain my project again. This project has two sections:
1) website section
2) REST API section for mobile app
For REST API section, I don’t load some files like theme files.
I can not use wp_set_object_terms() because this function first checks whether taxonomy exists or not. I checked existing of taxonomy with taxonomy_exists( $taxonomy ) and it returns false. I think it occurs because I turn off loading theme for REST API requests and custom taxonomies are not registered.
So, in this case, if I want to use wp_set_object_terms() function, I have to register custom taxonomies at first. I’m not sure whether using the register_taxonomy function can solve my problem or it makes more problems.
Another solution is the use of custom sql query and assigning custom tags and categories ids to custom posts.
I prefer second solution because I’m not sure about the use of register_taxonomy function.
Even assuming you correctly add terms through SQL queries, your plugin still needs to register any non-default taxonomies in order for the API to properly handle related requests. Things are setup behind the scenes for taxonomies beyond just having the correct DB records added.
Since you must register no matter what, I see little reason to not use set_object_terms(). If you prefer to use SQL queries anyway, study the relationships of the various taxonomy and term tables through phpMyAdmin. You must strictly adhere to the established relationships when you alter object terms in any manner.
Maybe you can create your own version of wp_set_object_terms() by copying the code from WordPress and removing the taxonomy exists conditional. Whether or not it’s registered it should still exist in the database and should still take care of making all the database connections for you.