Display all taxonomy based on post type

I just want to display a taxonomy term based on post type so that I can display and filter the items based on their taxonomy.

My code below displays EVERY taxonomy. I want it to be only a specific post type Publication

<?php
     $taxonomy = 'articletype';

     $tax_terms = get_terms( $taxonomy, array(
        'post_type' => 'publication'
     ) );

 ?>
 <?php
     foreach ($tax_terms as $tax_term) {
         ?>
     <button class="button" data-filter=".<?php echo $tax_term->slug;?>" >
         <?php echo $tax_term->name;?>
     </button>
 <?php } ?>

Related posts

2 comments

  1. Try using Codex Function Reference/get object taxonomies.

    Usage:

    <?php get_object_taxonomies( $object, $output ); ?>
    

    Parameters:

    $object: (array|string|object) (required):
    Name of the post type, or a post object (row from posts).

    $output: (string) (optional): The type of output to return, either taxonomy ‘names’ or ‘objects’.

    Return Values:

    (array): All taxonomy names or objects for the given post type/post object.

    also you can visit the original codex page to get examples and more info.

  2. I’m not sure if it’s still doable in your case (depends on if your site is still on dev or has too much content already), but I think it would be better to create custom taxonomy for your custom post type, it would make it easier for you to orgaanize your content.

    I tried to look for a function in the wp codex that sorts taxonomy by post type but I couldn’t find it.

Comments are closed.