0

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 } } } } ?>

2 Answers 2

0

Try Below code:

    $args = array(
                    'post_type'      => 'work',
                    'posts_per_page' => -1,
                    'post_status'    => 'publish'
                );

    $the_query = new WP_Query( $args );
    $my_categories = array();
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();

            $terms = get_the_terms( get_the_ID(), 'category' );
            if ( $terms && ! is_wp_error( $terms ) ) : 
                foreach ( $terms as $term ) {
                if(!in_array($term->term_id, $my_categories))
                    $my_categories[] = $term->term_id;
                }   
            endif;  
        }
        wp_reset_postdata();
    }

    if(sizeof($my_categories)) {
        foreach ($my_categories as $term_id) {
            $category = get_term_by('id', $term_id, 'category');
            if($category->slug!="all-articles") {
            ?>
                <button class="filter--item" data-filter=".<?php echo $category->slug; ?>"><?php echo $category->name; ?> <span class="checkbox"><i class="i-check"></i></span></button>
            <?php
            }
        }
    }

Hope this will help you!

0
0

You can use get_object_taxonomies() which will allow you to get taxonomies according to post type. Later you can follow the same procedure above.

3
  • So would I add code for get_object_taxonomies() just above my code in the same file?
    – egr103
    Dec 7, 2017 at 10:21
  • no. Use this function instead of get_taxonomies()
    – Sid
    Dec 7, 2017 at 10:33
  • 1
    Could you provide a code example for the functions.php as I have no experience with this?
    – egr103
    Dec 7, 2017 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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