Jun 16, 2016

Get Post Types by Taxonomy in WordPress

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.

/**
 * 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().