59

How can i get taxonomies of a post type?

If I have a post type event and i need to find out the list of taxonomies that are attached to that post type. How do I find them?

7 Answers 7

49

I think I've got it! After looking at couple of functions in the taxonomy.php file in WordPress I have found the function get_object_taxonomies(); which did the trick :)

7
  • 2
    See this for more info: codex.wordpress.org/Function_Reference/get_object_taxonomies Jun 21, 2011 at 13:15
  • wow... good to know about get_object_taxonomies(). it just helped me hijack template_redirect Nov 10, 2011 at 3:17
  • Hi thankx for this but how to order them by ID instead of NAME?
    – dh47
    Oct 19, 2015 at 7:08
  • easiest way will be just sort them using a for or foreach loop.
    – Sisir
    Oct 19, 2015 at 9:25
  • Yes I am fetching using foreach loop but I am getting order by name $taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) ); foreach( $taxonomies as $taxonomy ) : // Gets every "category" (term) in this taxonomy to get the respective posts $terms = get_terms( $taxonomy ); ?> <ul class="specials"><?php foreach( $terms as $term ) : ?> <li><h2 ><?php echo $term->name; ?></h2>
    – dh47
    Oct 19, 2015 at 10:19
9

get_categories will do the job.

get_categories('taxonomy=taxonomy_name&type=custom_post_type'); 
2
  • (I think if I understood the question right!) Jun 21, 2011 at 11:46
  • 4
    Thing is i don't have any taxonomy name, that's what i want to find out. I only have the name of the post type. By the post type name i want to find out all the taxonomy that are attached to it. Thanks anyway!
    – Sisir
    Jun 21, 2011 at 12:47
2

Apologies for raising an old post, but I came across this problem while looking for an answer for my use case.

I wanted to retrieve all available taxonomies for a post type, and also retrieve all available terms per taxonomy.

Thank you to Nick B for setting me in the right direction with his answer: https://wordpress.stackexchange.com/a/357448/198353

// get a list of available taxonomies for a post type
$taxonomies = get_taxonomies(['object_type' => ['your_post_type']]);

$taxonomyTerms = [];

// loop over your taxonomies
foreach ($taxonomies as $taxonomy)
{
  // retrieve all available terms, including those not yet used
  $terms    = get_terms(['taxonomy' => $taxonomy, 'hide_empty' => false]);

  // make sure $terms is an array, as it can be an int (count) or a WP_Error
  $hasTerms = is_array($terms) && $terms;

  if($hasTerms)
  {
    $taxonomyTerms[$taxonomy] = $terms;        
  }
}
2
  • This crashes my website
    – Pete
    Feb 5, 2021 at 7:35
  • In what sense does this code snippet crash your website? Unless you have an astronomical amount of taxonomies and terms, it's unlikely to be the fault of this snippet.
    – seemly
    Feb 10, 2021 at 13:21
1

Have you tried anything? something like this?

<?php 

$args=array(
  'object_type' => array('event') 
); 

$output = 'names'; // or objects
$operator = 'and'; // 'and' or 'or'
$taxonomies=get_taxonomies($args,$output,$operator); 
if  ($taxonomies) {
  foreach ($taxonomies  as $taxonomy ) {
    echo '<p>'. $taxonomy. '</p>';
  }
}
?>
2
  • 1
    Looked at get_taxonomies(); function on codex but it has very poor documentation and was no idea how i can pass the post types.
    – Sisir
    Jun 21, 2011 at 9:56
  • Sorry, this code is returning all registered taxonomies in wordpress.
    – Sisir
    Jun 21, 2011 at 9:59
1
$taxonomies = get_taxonomies( [ 'object_type' => [ 'custom_post_type' ] ] );
1

Use get_object_taxonomies (https://developer.wordpress.org/reference/functions/get_object_taxonomies/), which takes either the name of your custom post type or a post object as the parameter:

$taxonomies = get_object_taxonomies('custom_post_type');
$taxonomies = get_object_taxonomies($custom_post_object);

get_taxonomies() won't return any taxonomies that are used by multiple post types (https://core.trac.wordpress.org/ticket/27918).

1
get_post_taxonomies()

https://developer.wordpress.org/reference/functions/get_post_taxonomies/

This worked for me.

1
  • This function retrieves all taxonomy names for the given post, which is not what OP wants, but this is what I was actually looking for, thanks.
    – frzsombor
    Sep 26 at 13:45

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.