Check is custom post type

I have a custom post type :

// Custom posttype Events
$labels = array(
    'name' => _x('Events', 'Post Type General Name'),
    'singular_name' => _x('Events', 'Post Type Singular Name'),
    'menu_name' => __('Events'),
    'parent_item_colon' => __('Events:'),
    'all_items' => __('All Items'),
    'view_item' => __('View Item'),
    'add_new_item' => __('Add New Event'),
    'add_new' => __('Add New'),
    'edit_item' => __('Edit Item'),
    'update_item' => __('Update Item'),
    'search_items' => __('Search Item'),
    'not_found' => __('Not found'),
    'not_found_in_trash' => __('Not found in Trash'),
);
$args = array(
    'labels' => $labels,
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments', 'trackbacks', 'custom-fields',),
    'taxonomies' => array('post_tag'),
    'hierarchical' => false,
    'rewrite' => array('slug' => __('events')),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 10,
    'menu_icon' => 'dashicons-images-alt2',
    'can_export' => true,
    'has_archive' => false,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'capability_type' => 'post',
);
register_post_type('events', $args);

And a taxonomy for custom post type Events:

Read More
// Add new "Type" taxonomy to Events
    register_taxonomy('type-events', 'event', array(
        'hierarchical' => true,
        'labels' => array(
            'name' => _x( 'Types', 'taxonomy general name', 'my_theme' ),
            'singular_name' => _x( 'Types', 'taxonomy singular name', 'my_theme' ),
            'search_items' =>  __( 'Search Type', 'my_theme' ),
            'all_items' => __( 'All Types', 'my_theme' ),
            'parent_item' => __( 'Parent Type', 'my_theme' ),
            'parent_item_colon' => __( 'Parent Type:', 'my_theme' ),
            'edit_item' => __( 'Edit Type', 'my_theme' ),
            'update_item' => __( 'Update Type', 'my_theme' ),
            'add_new_item' => __( 'Add New Type', 'my_theme' ),
            'new_item_name' => __( 'New Type', 'my_theme' ),
            'menu_name' => __( 'Types', 'my_theme' ),
        ),
        // Control the slugs used for this taxonomy
        'rewrite' => array(
            'slug' => 'type-events', 
            'with_front' => false, 
            'hierarchical' => true
        ),
    ));

In Dashboard Admin, i create two taxonomy type event :
Taxonomies

Custom post type and taxonomy use the same template.

In file template, I want check if it is post type or taxonomy.
Currently, I use is_post_type_archive() to check, but the both return true. That is not what I need.

How to check if this is custom post type or taxonomy?

Related posts

3 comments

  1. If you want to check if a post is the custom post type events within the loop you can use this:

    <?php if ( get_post_type() === 'events' ) { 
        /* Do Stuff */ 
    } ?>
    

    If this is outside of the loop, you need to pass the post id into get_post_type():

    <?php if ( get_post_type( $post_id ) === 'events' ) { 
        /* Do Stuff */ 
    } ?>
    

    Edit

    You can test for multiple custom post types this way:

    <?php if ( get_post_type() === 'events' || get_post_type() === 'promos' || get_post_type() === 'courses' ) { 
        /* Do Stuff */ 
    } ?>
    
  2. This is little complicated, maybe the best way is to check with is_post_type_archive() and is_tax(), before you load the themplate, but in your case you may try with get_queried_object().

    Codex – get_queried_object

    The returned object will have properties taxonomy, term_id, and term_taxonomy_id if this is taxonomy.

    So maybe you can use something like get_queried_object()->taxonomy to determine if this is taxonomy and get its name and get_queried_object()->query_var for custom_post_type.

    if ( isset( get_queried_object()->taxonomy ) {
       //do taxonomy work ...
    }
    if ( property_exists( get_queried_object()->query_var ) {
       //post things 
    }
    
  3. function custom_post_type() {
        $singular="Car";
        $plural="cars";  
    
        $labels = array(
            'name'                  => _x( $plural, 'Post Type General Name', 'text_domain' ),
            'singular_name'         => _x( $singular, 'Post Type Singular Name', 'text_domain' ),
            'menu_name'             => __( $plural, 'text_domain' ),
            'name_admin_bar'        => __( $singular, 'text_domain' ),
            'archives'              => __( '$singular Archives', 'text_domain' ),
            'attributes'            => __( $singular.' Attributes', 'text_domain' ),
            'parent_item_colon'     => __( 'Parent '.$singular.':', 'text_domain' ),
            'all_items'             => __( 'All '.$plural, 'text_domain' ),
            'add_new_item'          => __( 'Add New '.$singular, 'text_domain' ),
            'add_new'               => __( 'Add New', 'text_domain' ),
            'new_item'              => __( 'New '.$singular, 'text_domain' ),
            'edit_item'             => __( 'Edit '.$singular, 'text_domain' ),
            'update_item'           => __( 'Update '.$singular, 'text_domain' ),
            'view_item'             => __( 'View '.$singular, 'text_domain' ),
            'view_items'            => __( 'View '.$singular, 'text_domain' ),
            'search_items'          => __( 'Search '.$singular, 'text_domain' ),
            'not_found'             => __( 'Not found', 'text_domain' ),
            'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
            'featured_image'        => __( 'Featured Image', 'text_domain' ),
            'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
            'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
            'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
            'insert_into_item'      => __( 'Insert into '.$singular, 'text_domain' ),
            'uploaded_to_this_item' => __( 'Uploaded to this '.$singular, 'text_domain' ),
            'items_list'            => __( $singular.' list', 'text_domain' ),
            'items_list_navigation' => __( $singular.' list navigation', 'text_domain' ),
            'filter_items_list'     => __( 'Filter '.$singular.' list', 'text_domain' ),
        );
        $supports =array('title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats');
        $texonomies =array('category', 'post_tag');
        $args = array(
            'label'                 => __( $singular, 'text_domain' ),
            'description'           => __( $singular.' Description', 'text_domain' ),
            'labels'                => $labels,
            'supports'              => $supports,
            'taxonomies'            => $texonomies,
            'hierarchical'          => false,
            'public'                => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'menu_position'         => 8,
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => true,
            'can_export'            => true,
            'has_archive'           => true,
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'capability_type'       => 'page',
            'menu_icon'             => 'dashicons-video-alt'`enter code here`
        );
        register_post_type( 'post_type', $args );
    
    }
    add_action( 'init', 'custom_post_type');
    

Comments are closed.