WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have multiple custom tags setup (ptags) and custom posttypes (products) and made it so i can filters posts by multiple tags in a category.

Currently the code retrieves tags from posts relating to the active category (pcategory) I have dropdown boxes from general to specific e.g.: <BRAND> <TYPE> <SIZE>

When a visitor selects an option, the info is posted to the current page (category page the user is on) and filters posts on that tag.

Once that happens i want the tags removed from the dropdown box that are not active anymore on this category listing.

If i select brand "A" and the page refreshes i don't want to show the sizes that do not apply to any of the posts.

I hope you understand what i am trying to say. Maybe this can be solved by using AJAX or adjusting SQL query?

My template code looks like this:

<form action="" method="get"><div>
$current_term1 = $wp_query->get_queried_object();   
$args = array(
'categories'                => $current_term1->term_id,
'taxonomy'                  => "brand"
$outputbrand = get_category_tags_test($args);

$outputbrand = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $outputbrand);
echo $outputbrand;?>

$args = array(
'categories'                => $current_term1->term_id,
'taxonomy'                  => "type"
$outputtype = get_category_tags_test($args);

$outputtype = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $outputtype);
echo $outputtype;?>
<input type="submit" value="Filter" />

My functions.php code looks like this:

//begin test
function get_category_tags_test($args) {
global $wpdb;
$tags = $wpdb->get_results
    SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link, terms2.slug as tag_slug
        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
        t1.taxonomy = 'pcategory' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
        t2.taxonomy = (".$args['taxonomy'].") AND p2.post_status = 'publish'
        AND p1.ID = p2.ID
    ORDER by tag_name
$output1 ="<select name=".$args['taxonomy'].">";
$output1 .='<option value="">'.$args['taxonomy'].'</option>';
foreach ($tags as $tag) {
    $term_name =$tag->tag_name;
    $link = $term_slug;
    $output1 .="<option value='".$link."'>".$term_name."(".$term->count.")</option>";
$output1 .="</select>";
return $output1;
} //test end

If there is any plugin that can do this you can also mention it because i could not find anything. :) Thanks

share|improve this question
up vote 2 down vote accepted

I think the best way to do this is using the loop, in the loop you need to get the terms for all associated posts.

<?php if (have_posts()) :
<while (have_posts()) : the_post();
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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