Joshua David Nelson

Bullshit Free WordPress Development

  • Services
  • Code
  • About
  • Contact

Get Post Types By Taxonomy

Get post types that have a specific taxonomy, returns an array of objects or names, or false if nothing found.

<?php
/**
* Get post types that have a specific taxonomy (a combination of get_post_types and get_object_taxonomies)
*
* @author Joshua David Nelson, [email protected]
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2.0
*
* @see register_post_types(), get_post_types(), get_object_taxonomies()
*
* @param string $taxonomy Required. The name of the taxonomy the post type(s) supports.
* @param array | string $args Optional. An array of key => value arguments to match
* against the post type objects. Default empty array.
* @param string $output Optional. The type of output to return in the array.
* Accepts post type 'names' or 'objects'. Default 'names'.
*
* @return array | boolean A list of post type names or objects that have the taxonomy
* or false if nothing found.
*/
if( !function_exists( 'get_post_types_by_taxonomy' ) ) {
function get_post_types_by_taxonomy( $taxonomy, $args = array(), $output = 'names' ) {
// Get all post types
$post_types = get_post_types( $args, $output );
// We just need the taxonomy name
if( is_object( $taxonomy ) ){
$taxonomy = $taxonomy->name;
// If it's not an object or a string, it won't work, so send it back
} elseif( !is_string( $taxonomy ) ) {
return false;
}
// setup the finished product
$post_types_with_tax = array();
foreach( $post_types as $post_type ) {
// If post types are objects
if( is_object( $post_type ) ) {
$taxonomies = get_object_taxonomies( $post_type->name, 'names' );
if( in_array( $taxonomy, $taxonomies ) ) {
$post_types_with_tax[] = $post_type;
}
// If post types are strings
} elseif( is_string( $post_type ) ) {
$taxonomies = get_object_taxonomies( $post_type, 'names' );
if( in_array( $taxonomy, $taxonomies ) ) {
$post_types_with_tax[] = $post_type;
}
}
}
// If there aren't any results, return false
if( empty( $post_types_with_tax ) ) {
return false;
} else {
return $post_types_with_tax;
}
}
}
view raw get-post-type-by-taxonomy.php hosted with ❤ by GitHub

Source: https://gist.github.com/joshuadavidnelson/56d73c284490f9ef21a5

Tags: custom post type, Taxonomy

Previous Code:
Set Genesis Theme Settings Defaults
Back to the Code Snippets
Next Code:
Exclude Post Types From Search
  • Twitter
  • RSS Feed URL

About Me

I'm a WordPress Engineer. I build sleek, custom websites with WordPress and Genesis.

See my services and my recent work.

Contact me to get your project started.

Gravity Forms Plugin for WordPress Fastest WordPress Hosting

Recent Posts

  • Using Font Awesome Icons for WooCommerce Grid / List Toggle
  • Disable Blog: WordPress Gone Blog-less
  • Category (Taxonomy) Dropdown Filtered By Post Type
  • Weather in WordPress with Dark Sky
  • Fixing Your Deprecated Widget Constructors in WordPress 4.3
  • Twitter
  • RSS Feed URL
  • Code Snippets
  • My Plugins
  • Make a Payment

© Joshua David Nelson | Hand-Forged | WordPress + Genesis | Terms of Service | Legal | Contact