WordPress function to check if the current post is a custom post type

Simply paste this code into your functions.php file:

function is_custom_post_type() {
	global $wp_query;
		
	$post_types = get_post_types(array('public'   => true,'_builtin' => false),'names','and');
	
	foreach ($post_types  as $post_type ) {
		if (get_post_type($post_type->ID) == get_post_type($wp_query->post->ID)) {
			return true;
		} else {
			return false;
		}
	}
}

Once done, you can use the function as shown below. Please note that the function can be used outside the loop:

if (is_custom_post_type()) {
    //Current post is a custom post type
}

Thanks to Jonas Ethomsen for the function!

Leave a Reply

Your email address will not be published. Required fields are marked *