WordPress Snippets at WPcustoms

Conditional check for all custom post types

By default we can use ” if ( is_singular( ‘book’ ) )” to check for a specific custom post type. But what to do if we need a conditional tag for any non-built-in custom post types? This function filters it and returns true if a custom post type is found. Use like any other conditional tag


/**
 * Snippet Name: Conditional check for all custom post types
 * Snippet URL: http://www.wpcustoms.net/snippets/conditional-check-custom-post-types/
 */
 // usage: if ( is_custom_post_type() ) {
//   print 'This is a custom post type!'; }

function is_custom_post_type( $post = NULL )
{
    $all_custom_post_types = get_post_types( array ( '_builtin' => FALSE ) );

    // there are no custom post types
    if ( empty ( $all_custom_post_types ) )
        return FALSE;

    $custom_types      = array_keys( $all_custom_post_types );
    $current_post_type = get_post_type( $post );

    // could not detect current type
    if ( ! $current_post_type )
        return FALSE;

    return in_array( $current_post_type, $custom_types );
}