Get Post Types by Taxonomy in WordPress

June 16, 2016

The code below can be used to get the post types for a taxonomy in WordPress. This is useful if you know the taxonomy and want to get the post types associated with it.

<?php
/**
* Get all post types for a taxonomy.
*
* @author Kellen Mace
* @param string $taxonomy The taxonomy slug to get post types for.
* @return array The post types associated with $taxonomy.
*/
function wds_campbell_get_post_types_by_taxonomy( $taxonomy = '' ) {
global $wp_taxonomies;
if ( isset( $wp_taxonomies[ $taxonomy ] ) ) {
return $wp_taxonomies[ $taxonomy ]->object_type;
}
return array();
}

To do the opposite of this (get the taxonomies for a post type), see get_object_taxonomies().


Kellen Mace

Written by Kellen Mace, who lives in Rochester Hills, MI and builds cool stuff on the web. About Kellen // Follow him on Twitter →