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 /
thx in advance
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/how-to-display-wordpress-tags-dropdown-menu/
thx in advance
Share Improve this question asked Mar 21, 2011 at 12:24 FuxiFuxi 7695 gold badges13 silver badges18 bronze badges 2- This shows you how to get tags for a category: wprecipes/… – anu Commented Mar 21, 2011 at 12:31
- 1 I had same issue with custom taxonomies. Here is a function to use: <gist.github/sergeliatko/da8f99a1883f3a9c02fef827160f7aee> – Serge Liatko Commented May 29, 2017 at 19:59
3 Answers
Reset to default 6Late answer
There's get_terms()
here for that. No need for any custom queries.
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,
) );
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