SQL to get WooCommerce product categories (terms)

For an import script I needed to get WooCommerce product categories so I ended up looking the generated query in /var/log/mysql/query.log for get_the_terms($postID, ‘taxonomy_name’)

I ended up using the following SQL query to get product categories:

SELECT t.*, tt.*
FROM wp_terms AS t 
INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id 
INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.taxonomy IN ('product_cat') AND tr.object_id IN (11448)
Advertisements

Author: Michal Zuber

Full stack developer, biker and rollerblader. Owner and developer at https://nevilleweb.sk/ Co-founded http://neville.sk/ Blog at https://michalzuber.wordpress.com/

2 thoughts on “SQL to get WooCommerce product categories (terms)”

  1. Hi, I used your query to get all woocommerce categories but since the last maj of WP I think, it doesn’t work

    Here’s my query :

    SELECT t.term_id, t.name, t.slug, t.term_order, tt.term_taxonomy_id, tt.count FROM wp_terms t LEFT JOIN wp_term_taxonomy as tt ON t.term_id = tt.term_id WHERE tt.taxonomy = ‘product_cat’ ORDER BY t.term_order

    The issue is : #1054 – Unknown column ‘t.term_order’ in ‘field list’

    Do you have the same error ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s