Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
<?php
$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;?>

<?php
$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" />
</div></form>

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
    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 = '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_slug=$tag->tag_slug;
    $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

1 Answer 1

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();
//thecode
    endwhile;
    endif;
?>
share|improve this answer

Your Answer

 
discard

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.