0

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

SELECT * FROM wp7r_terms AS t
JOIN wp7r_term_taxonomy AS tt ON tt.term_id = t.term_id
WHERE t.term_id = 141

returns the corect data. When I use get_term(107) where 107 is term_id of a product category I get correctly WP_Term object. What am I missing?

5
  • What do you mean by “ my own taxonomy which has in the table terms corresponding row with a term”? Are you using custom table? Or inserting data directly into the table? When are you attempting to use get_term()? Have you registered your taxonomy? Sep 22 at 11:47
  • wp7r_term_taxonomy table contains term_taxonomy_id = 141, term_id = 141, taxonomy = 'shop_coupon_cat' and wp7r_terms table contains term_id = 141, name = 'Birthday' and slug = 'birthday'. I created it by inserting the data into DB.
    – Vlado
    Sep 22 at 12:16
  • 1
    You need to register the taxonomy with WordPress: developer.wordpress.org/reference/functions/register_taxonomy You can't just use an arbitrary value as the taxonomy. Sep 22 at 12:20
  • Understood. But the registration has to create something in a table. The question is: where? I am sure that there is a way to create it direct in DB.
    – Vlado
    Sep 22 at 13:07
  • taxonomies are registered at runtime, not in the database, if you don't call register_taxonomy during a request to WordPress it will be unaware of that custom taxonomy. Note that directly inserting data into the WP database tables has compatibility issues, it won't clear caches, update term counts, or fire plugin hooks and filters which can break stuff. It would be much easier and safer to do this using WP CLI, WP functions such as wp_insert_term, or REST API calls. What you're doing by using raw SQL to insert new terms is heavily discouraged
    – Tom J Nowell
    Sep 22 at 13:41

1 Answer 1

2

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:

  • call 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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.