I am using my own taxonomy which has in the table terms corresponding row with a term. I need to get the term by term_id using get_term(141) but i get error invalid_taxonomy. I think that the relationships are correct because this query
If WordPress is not aware of a taxonomy and you try to grab a term in that taxonomy it will return an error, even if a term with that ID exists in the database.
All taxonomies must be registered at runtime using the
register_taxonomy function. It's the same for custom post types. There is no list of taxonomies and post types or table that lists these in the database. At the start of a request WordPress has no post types or taxonomies, not even posts/tags/categories. It has to register them internally and they're stored in PHP global variables.
For this reason, you can't create new taxonomies using an SQL insert and expect them to show up in WordPress or work with WordPress APIs.
You'd need to maintain a list of your new taxonomies somewhere, then write PHP code to load the list and call
register_taxonomy on the
init hook of every request.
You should also be aware that directly modifying the database like that carries risks you might not know about. For example adding a post to a term with raw SQL queries will not update term caches and post counts, won't invalidate and regenerate caches, etc. This means your term might not show any posts despite you changing the database, posts might not report that they have the term you used, etc.
It also means your system might have fundamental incompatiblity with page caching solutions and object caches ( a persistent object cache can have enormous performance benefits ).
It also prevents plugins from running their hooks, e.g. adding in meta fields, flushing caches, making requests, syncing things to other sites, etc etc
If you need to insert a new term, you should:
wp_insert_term, this might require bootstrapping WordPress so that its API is available if you're doing this from a non WordPress PHP application
- calling the REST API endpoint for the taxonomy to create a new term, a
POST request to
example.com/wp-json/wp/v2/tags for example to create a tag term, making sure it's an authenticated request. You will want
show_in_rest set to true when registering any taxonomies for this.
- Using WP CLI to do it, you can execute a system command from PHP or another language to create terms e.g.
wp term create category Apple --description="A type of fruit"