6

i'm displaying all posts by category in my template and i was wondering: is it possible to get a list of all tags used by that category?

i only found out how to make a tag-dropdown but it's from all articles, i couldn't find out yet how to filter it by category - any ideas?

here's the link http://wphacks.com/how-to-display-wordpress-tags-dropdown-menu/

thx in advance

2

3 Answers 3

6

Late answer

There's get_terms() here for that. No need for any custom queries.

2
  • Just adding, yes. This is the way to use this.
    – erwstout
    Commented Oct 30, 2015 at 18:40
  • 2
    Right answer but, you know...you should put an example for users passing by searching for the correct solution :)
    – Erenor Paz
    Commented Mar 15, 2017 at 15:13
1

Maybe this is late but thinks it will help someone. This is the code that gives you tags list for specific category

$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
1
  • This is not an answer to OP question
    – DevWL
    Commented Jan 9, 2022 at 2:39
0

10 year later but still better than never...

Categories and tags are not tied to each other. They are both related to post / product. Therefore, you need to query for the all posts/product and grab their IDs, so you can then query for tags where product/post ID is with in this set of IDs.

See the query below for WordPress Posts:

SET @CategoryByID = 1; -- set correct category ID
-- SET @CategoryByName = 'Politics'; -- set correct category name

SELECT DISTINCT t.* FROM wp_posts AS p
LEFT JOIN wp_term_relationships tr ON p.ID = tr.object_id
LEFT JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN wp_terms t ON t.term_id = tt.term_id
WHERE p.post_type="post" -- <<< for wordpress post type
AND p.post_status = 'publish'
AND tt.taxonomy = "post_tag" -- <<< for wordpress post tags
AND p.ID IN
    (SELECT p.ID FROM wp_posts AS p
    LEFT JOIN wp_term_relationships tr ON p.ID = tr.object_id
    LEFT JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
    LEFT JOIN wp_terms t ON t.term_id = tt.term_id
    WHERE p.post_type="post" <<< for wordpress post type
    AND p.post_status = 'publish'
    AND tt.taxonomy = "category" -- <<< for wordpress post category
    AND tt.term_taxonomy_id = @CategoryByID -- <<< search category by id (use one)
    -- AND t.name = @CategoryByName  -- <<< search category by name (use one)
    ORDER BY p.ID)
ORDER BY p.ID

See the example query below for WooCommerce Products:

SET @CategoryByID = 18; -- set correct category ID
-- SET @CategoryByName = 'T-SHIRT'; -- set correct category name

SELECT DISTINCT t.* FROM wp_posts AS p
LEFT JOIN wp_term_relationships tr ON p.ID = tr.object_id
LEFT JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN wp_terms t ON t.term_id = tt.term_id
WHERE p.post_type="product" -- <<< for woocommerce post type
AND p.post_status = 'publish'
AND tt.taxonomy = "product_tag" -- <<< for woocommerce tags
AND p.ID IN
    (SELECT p.ID FROM wp_posts AS p
    LEFT JOIN wp_term_relationships tr ON p.ID = tr.object_id
    LEFT JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
    LEFT JOIN wp_terms t ON t.term_id = tt.term_id
    WHERE p.post_type="product"
    AND p.post_status = 'publish'
    AND tt.taxonomy = "product_cat" -- <<< for woocommerce products
    AND tt.term_taxonomy_id = @CategoryByID -- <<< search category by id (use one)
    -- AND t.name = @CategoryByName  -- <<< search category by name (use one)
    ORDER BY p.ID)
ORDER BY p.ID

Your Answer

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

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