So I have a Custom Post Type with some Custom Taxonomies.

On the archive page where I list all the Custom Post Types I have an refine search in the sidebar which allows you to tick a Taxonomy Term and then search for Post Types with that Term. (Image Below)

Redefine Search

When you click say ‘Cellar’ it reloads the page and shows you the result (in this instance it’s only one Post Type)

My question is: How do I update the numbers on the side of the names so if ‘Cellar’ is checked then the number next to ‘Carpet’ should update to say (1) because there is only 1 property with the ‘Cellar’ and ‘Carpet’ term together

Here is the code controlling the output of the number but at the moment this is just static and is just count every Post Type with that term.

<?php
$all_features = get_terms('features');

/* Features in search query */
$required_features_slugs = array();
if( isset ( $_GET['features'] ) ) {
    $required_features_slugs = $_GET['features'];
}

$features_count = count ($all_features);
if($features_count > 0) {
    ?>
    <div class="more-options-wrapper clearfix">
        <?php
        foreach ($all_features as $feature ) {
            ?>
            <div class="option-bar">
                <input type="checkbox"
                       id="feature-<?php echo $feature->slug; ?>"
                       name="features[]"
                       value="<?php echo $feature->slug; ?>" onclick="document.getElementById('refine-properties').submit();"
                    <?php if ( in_array( $feature->slug, $required_features_slugs ) ) { echo 'checked'; } ?> />
                <label for="feature-<?php echo $feature->slug; ?>"><?php echo $feature->name; ?> <small>(<?php echo $feature->count; ?>)</small></label>
            </div>
            <?php
        }
        ?>
    </div>
<?php
}
?>

I have put an example below:

enter image description here
enter image description here

The Images above represent what I am trying to do

2 Answers
2

One way which would seem inefficient ( but at the moment I cannot think of a way without re-querying the database ) is to Query each term as you loop. You’ll be making many more queries so we’ll limit what is returned to try and speed them up.

if ( $features_count > 0 ) {
  ?>

    <div class="more-options-wrapper clearfix">

      <?php foreach ($all_features as $feature ) {
            $count = $feature->count;

            if( ! empty( $required_features_slugs ) ) {
                $tmp_required = $required_features_slugs;

                if( ! in_array( $feature->slug, $required_features_slugs ) ) {
                    array_push( $tmp_required, $feature->slug );
                }

                $tmp_query = new WP_Query( array(
                    'posts_per_page'    => -1,
                    'fields'            => 'ids',           // Only return post IDs
                    'tax_query'         => array( array(
                        'taxonomy'  => 'features',
                        'field'     => 'slug',
                        'terms'     => $tmp_required,
                    ) ),
                ) );

                $count = ( $tmp_query->have_posts() ) ? count( $tmp_query->posts ) : 0;
            }
      ?>

            <div class="option-bar">
                <input type="checkbox"
                       id="feature-<?php echo $feature->slug; ?>"
                       name="features[]"
                       value="<?php echo $feature->slug; ?>" onclick="document.getElementById('refine-properties').submit();"
                    <?php if ( in_array( $feature->slug, $required_features_slugs ) ) { echo 'checked'; } ?> />
                <label for="feature-<?php echo $feature->slug; ?>"><?php echo $feature->name; ?> <small>(<?php echo $count; ?>)</small></label>
            </div>

      <?php } ?>

    </div>

  <?php
}

What we’re doing is that if one or more categories are selected we’re going to loop through the categories and append the current slug. Then we’re querying the database for posts that have our required categories plus the current category. Finally, we only return post IDs for speed purposes – if we have posts we count them otherwise it’s 0.

Leave a Reply

Your email address will not be published. Required fields are marked *