Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am looking for a way to test if a post is a custom post type. For example, in say the sidebar I can put code like this:

 if( is_single()) {
        //code here
    }

I want code were I could do this, only testing for a custom post type. Any help would be greatly appreciated!

share|improve this question

6 Answers 6

up vote 47 down vote accepted

Here you are: get_post_type() and then if ( 'book' == get_post_type() ) ... as per Conditional Tags > A Post Type in Codex.

share|improve this answer
    
Why that's exactly what I needed. Thank you. –  Adam Thompson Jan 11 '11 at 17:17
15  
    
Superb this what i want :) +10000 –  Fresher Nov 19 '13 at 11:20
if ( is_singular( 'book' ) ) {
    // conditional content/code
}

The above is TRUE when viewing a post of the Custom Post Type: book.

if ( is_singular( array( 'newspaper', 'book' ) ) ) {
    //  conditional content/code
}

The above is true when viewing a post of the Custom Post Types: newspaper or book.

These are more conditional tags can be viewed here.

share|improve this answer
    
I won't be surprise if this thread has protected :) –  Fresher Nov 19 '13 at 11:21

For anyone else looking for this, add this to your functions.php and you can have the functionality, inside or outside of the loop:

function is_post_type($type){
    global $wp_query;
    if($type == get_post_type($wp_query->post->ID)) return true;
    return false;
}

so you can now use the following:

if (is_single() && is_post_type('post_type')){
  //work magic
}
share|improve this answer
    
Thank you, this is very useful! But it should be: if (is_single() && is_post_type('post_type')){ //work magic } The closing bracket was missing.... Many greetings, Ethel –  user10462 Nov 21 '11 at 14:23

To test if a post is any custom post type, fetch the list of all not built-in post types and test if the post’s type is in that list.

As a function:

/**
 * Check if a post is a custom post type.
 * @param  mixed $post Post object or ID
 * @return boolean
 */
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 );
}

Usage:

if ( is_custom_post_type() )
    print 'This is a custom post type!';
share|improve this answer

If for any reason you already have access to the global variable $post, you can simply use

if ($post->post_type == "your desired post type") {
}
share|improve this answer

If you want a wild card check for all your custom post types:

if( ! is_singular( array('page', 'attachment', 'post') ) ){
    // echo 'Imma custom post type!';
}

This way you don't need to know the name of your custom post. Also the code still work even if you change the name of your custom post later.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.