My custom post type & built in Wordpress blog posts SHARE the built in categories.
I have a piece of code that loops through and shows all (built in) categories that have posts assigned to them on 'archive-work.php'
I only want to show the categories that have CPT (work) posts assigned to them and NOT show any other posts (i.e. any blog posts that may be in the same category). However, my code below is showing all categories that have any post from any post type, how do I fix this to only show categories from the Work CPT?
<?php
$args=array(
'name' => 'category',
'public' => true,
'_builtin' => true
);
$output = 'names'; // or objects
$operator = 'and';
$taxonomies=get_taxonomies($args,$output,$operator);
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {
$terms = get_terms([
'post_type' => array( 'work' ),
'taxonomy' => $taxonomy,
'hide_empty' => 1,
]);
foreach ( $terms as $term) {
if ($term->slug == 'all-articles') {} else {?>
<button class="filter--item" data-filter=".<?php echo $term->slug; ?>"><?php echo $term->name; ?> <span class="checkbox"><i class="i-check"></i></span></button>
<?php } } } } ?>