mark-in-motion
mark-in-motion

Reputation: 293

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

Upvotes: 4

Views: 1359

Answers (2)

Shady Alset
Shady Alset

Reputation: 5714

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.

Upvotes: 2

Beirdo Studio
Beirdo Studio

Reputation: 43

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.

Upvotes: 1

Related Questions