Skip to content

Instantly share code, notes, and snippets.

@zarei-dev
Last active January 19, 2022 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zarei-dev/93ae195c2517ef7bbc8343fe8b627a0d to your computer and use it in GitHub Desktop.
Save zarei-dev/93ae195c2517ef7bbc8343fe8b627a0d to your computer and use it in GitHub Desktop.
woocommerce attributes sql query
SELECT
t.term_id AS 'Attribute Value ID',
tt.taxonomy AS 'Attribute Name',
wat.attribute_label AS 'Attribute Label',
t.name AS 'Attribute Value',
COUNT(t.name) as 'num'
FROM
wp_posts AS p
INNER JOIN wp_term_relationships AS tr
ON
p.ID = tr.object_id
INNER JOIN wp_term_taxonomy AS tt
ON
tr.term_taxonomy_id = tt.term_id AND tt.taxonomy LIKE 'pa_%'
INNER JOIN wp_terms AS t
ON
tr.term_taxonomy_id = t.term_id
INNER JOIN wp_woocommerce_attribute_taxonomies AS wat
ON
TRIM('pa_' FROM tt.taxonomy) = wat.attribute_name
WHERE
p.post_type = 'product' AND p.post_status = 'publish'
GROUP BY t.term_id, t.name, tt.taxonomy, wat.attribute_label;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment