everyone. I have 10 custom post types and they are sharing 5 taxonomies. Right now I am working on the archive page template where I want to create ajax filters based on taxonomies for each of the post types.
For example, we have post types Jewelry and Watches. Both of them have shared taxonomy Brand (by shared I mean, that every time I add the brand to the watches, it will also show up on the jewelry brands choice). Now, on the archive page of the watches, I want to show only brands that used by the watch custom posts.
To create a taxonomy I used the CPT UI plugin.
This is the code I came up with, but it shows all the terms:
<?php
// Retrieve the taxonomies for your custom post type
$cpt_taxes = get_object_taxonomies($current_post_type->name, 'object');
// Build an array of taxonomies slugs to be used in our $args array below to filter only taxes we need.
foreach ($cpt_taxes as $cpt_tax) {
$cpt_tax_name = $cpt_tax->name;
$cpt_tax_label = $cpt_tax->labels->singular_name;
?>
<li class="accordion-item " data-accordion-item>
<!-- Accordion tab title -->
<a href="#" class="accordion-title"><span><?php echo $cpt_tax_label; ?></span></a>
<!-- Accordion tab content -->
<div class="accordion-content" data-tab-content>
<?php
foreach ((array)get_terms(array(
'taxonomy' => $cpt_tax_name,
'orderby' => 'name',
'hide_empty' => true
)) as $term_data) {
echo '<input id="' . $term_data->name . '" type="checkbox" name="' . $term_data->name . '"><label for="' . $term_data->name . '">' . $term_data->name . ' <small>(' . $term_data->count . ')</small></label> <br>'; // ID of the category as the value of an option
}?>
</div>
</li>
<?php } ?>