WordPress SQL – List all child categories of specific parent category ID

If you need to list all categories listed under a specific parent category, you can run recursive SQL query. See the example below:

SET @parentCategoryId := 17; -- <<< parent post ID here

WITH recursive cteCategory(term_id, parent, name, taxonomy, slug) AS
(   
    SELECT t.term_id, tt.parent, t.name, tt.taxonomy, t.slug FROM wp_terms t
    LEFT JOIN wp_term_taxonomy tt 
    ON tt.term_taxonomy_id = t.term_id
    WHERE tt.term_taxonomy_id = @parentCategoryId 
    AND tt.taxonomy = 'category' -- <<< use "product_cat" for woocommerce products category
    UNION ALL
    SELECT t2.term_id, tt2.parent, t2.name, tt2.taxonomy, t2.slug FROM wp_terms t2
    LEFT JOIN wp_term_taxonomy tt2 
    ON tt2.term_taxonomy_id = t2.term_id
    INNER JOIN cteCategory
    ON tt2.parent = cteCategory.term_id
)


SELECT * FROM cteCategory; -- include parent category id
-- SELECT * FROM cteCategory cte WHERE cte.term_id != @parentCategoryId; -- only childrens

Output:

+---------+--------+---------------------+-------------+---------------------+
| term_id | parent | name                | taxonomy    | slug                |
+---------+--------+---------------------+-------------+---------------------+
|      17 |      0 | Warzywa             | category    | warzywa             |
|      56 |     17 | Pomidory            | category    | pomidory            |
|      57 |     56 | Pomidory koktailowe | category    | pomidory-koktailowe |
+---------+--------+---------------------+-------------+---------------------+
3 rows in set (0.001 sec)

Select top level categories

-- Query all categories those empty and those with parent category class
SELECT t.term_id, t.name, t.slug,  tt.taxonomy, tt.parent, tt.count FROM wp_terms t
LEFT JOIN wp_term_taxonomy tt ON t.term_id = tt.term_taxonomy_id
WHERE tt.taxonomy = "category" -- <<<
AND tt.parent = 0 -- only top level categories
-- AND tt.count = > 0 -- not empty
ORDER BY tt.taxonomy;

Output

+---------+-----------+-----------+-------------+--------+-------+
| term_id | name      | slug      | taxonomy    | parent | count |
+---------+-----------+-----------+-------------+--------+-------+
|      16 | Nowe      | nowe      | category    |      0 |     0 |
|      17 | Warzywa   | warzywa   | category    |      0 |     4 |
|      18 | Owoce     | owoce     | category    |      0 |     3 |
|      19 | Soki      | soki      | category    |      0 |     0 |
|      20 | Przetwory | przetwory | category    |      0 |     0 |
+---------+-----------+-----------+-------------+--------+-------+
5 rows in set (0.001 sec)

You can reed more about recurion here: https://mariadb.com/kb/en/recursive-common-table-expressions-overview/ and here How to create a MySQL hierarchical recursive query?

0
Would love your thoughts, please comment.x
()
x