139

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 in code like this:

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

I want code testing for only a custom post type.

8 Answers 8

165

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

4
  • 27
  • 3
    @Rarst This only apply to the single post template of a CPT. get_post_type() is the right way to go in a more broad approach. (eg: on a search page) Dec 26, 2020 at 20:40
  • 1
    @amarinediary I think at the time the question was unclear if the interest is in checking main page-defining post or just some post in an arbitrary loop. The appropriate way to express with template tag would vary accordingly. :)
    – Rarst
    Jan 4, 2021 at 10:01
  • is_singular() - more fastes... 0.000001 sec. get_post_type() - 0.000637 sec Feb 22, 2022 at 6:46
194
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 and more conditional tags can be viewed here.

0
34

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!';
3
  • 4
    This should be the accepted answer.
    – aalaap
    Mar 21, 2018 at 13:33
  • any idea how to check the post type in the backend?
    – evavienna
    Mar 6, 2021 at 10:24
  • 1
    @evavienna This code works everywhere. You just need a post object.
    – fuxia
    Mar 6, 2021 at 14:44
30

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
}
4
  • 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, 2011 at 14:23
  • Has this stopped working for anyone else? I've used this for ages, but suddenly this stopped working for me. However, using the same method without global $wp_query always works: if ( 'post-type' == get_post_type() ) {} Jan 13, 2017 at 13:39
  • 1
    is_post_type() is depreciated.
    – Lisa
    Dec 21, 2017 at 12:23
  • how do I get the slug from get_post_type($wp_query->post->ID) is it possible to do something like this get_post_type($wp_query->post->SLUG)
    – Ice_mank
    Mar 20, 2022 at 16:18
15

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") {
}
9

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.

0

To determine the $post_type easily inside a function, you need to call the global post var first, here is an example:

function the_views() {
global $post;

if ($post->post_type == "game") {
   $span = '<span class="fa-gamepad">';
  } else { // Regular post
    $span = '<span class="fa-eye">';
  }
}
0

I have been reading all the answers, they are usable, but the most simple that you can use to check if the post is standard or custom, you can just go with:

is_singular('post') or !is_singular('post') 

The first expression is for standard post, and the second one is for any custom post.

I hope it can help someone.

2
  • I think there are already a couple of answers for is_singular()
    – Rup
    Mar 2, 2022 at 15:33
  • Hello @Rup, yes it is, but none of the answers are describing how to select non-standard post eg -> !is_singular('post') is the simplest solution. Mar 3, 2022 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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