Skip to content

Instantly share code, notes, and snippets.

@chriscoyier
Created May 14, 2011 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chriscoyier/972314 to your computer and use it in GitHub Desktop.
Save chriscoyier/972314 to your computer and use it in GitHub Desktop.
is_custom_post_type.php
<?php
// returns boolean
function is_custom_post_type( $type = '' ) {
// access to currently queried post
global $post;
// get the current post type
$post_type = get_post_type( $post );
// all post types that aren't custom
$types = array("post", "page", "revision", "attachment");
// check if current post type is custom
if ( $type == '' && in_array($type, $types)) {
return true;
// if parameter passed, compare against that
} else if ( $type == $post_type ) {
return true;
} else {
return false;
}
}
?>
@joshuadavidnelson
Copy link

This is missing "nav_menu_item" as a default post type. Wouldn't it be easier to use the builtin argument in get_post_types() to create an array of all non-builtin post types? This would give you all custom post types (unless, of course, they were created as a builtin, but that would be bad practice). Then you wouldn't need the type argument, you could just use the array $types with your $post_type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment