0

I need some help writing a shortcode to display all product tags associated with a specific product category which can be displayed on any page.

My problem is that I can display all product tags anywhere by using the below code, but I'm unable to filter those tags by a given product category:

function product_terms_by_cat() {       
    $terms = get_terms (
        array (
            'taxonomy' => 'product_tag', 
            'hide_empty' => false)
        );
    foreach ( $terms as $term ) {
        echo $term->name;
    }
}       
add_shortcode('product_terms_by_cat', 'product_terms_by_cat');

I've tried using functions for standard WP post tags/categories but they're used within the loop (eg. category pages or single post pages) and don't help.

Could modifying the code found on this link work?The code filters categories based on tags (the opposite of what I want). Cheers

0
1

Ahh ha! Have figured out how to do this. Here's the code for future people who are looking for the same thing:

function get_category_tags($args) {
    global $wpdb;
    $tags = $wpdb->get_results("
        SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, terms2.slug as tag_slug, null as tag_link
        FROM
            wp_posts as p1
            LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
            LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
            LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,

            wp_posts as p2
            LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
            LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
            LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
        WHERE
            t1.taxonomy = 'product_cat' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND 
            t2.taxonomy = 'product_tag' AND p2.post_status = 'publish'
            AND p1.ID = p2.ID
        ORDER by tag_name
    ");

    $count = 0;

    foreach ($tags as $tag) {
        $tags[$count]->tag_link = get_tag_link($tag->tag_id);
        $count++;
    }
    return $tags;
}

$args = array('categories'=> "YOUR PRODUCT CATEGORY ID HERE"); 
$tags = get_category_tags($args); 

foreach ($tags as $tag) {   
    echo '<a href="'.$tag->tag_link.'">' . $tag->tag_name . '</a>';  
}

I used this in a WP Bakery shortcode and it worked a treat. I guess you could use this wherever you would like. Enjoy!

0
function product_terms_by_cat() {       
$terms = get_terms (
    array (
        'taxonomy' => 'product_category', 
        'hide_empty' => false)
    );
foreach ( $terms as $term ) {
    echo $term->name;
}
}       
add_shortcode('product_terms_by_cat', 'product_terms_by_cat');
1
  • Thanks, but I don't think you understood my problem. That snippet looks like it's the same as I had provided, but simply changed 'taxonomy' => 'product_tag', to 'taxonomy' => 'product_category',. It didn't help sorry. – JVAN Aug 19 '19 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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