How to Check if a Post Type Exists in WordPress
The function post_type_exists
checks if a post type is registered in WordPress.
Syntax
post_type_exists($post_type)
returns TRUE
if $post_type
is the identifier of a registered post type, otherwise returns FALSE
.
Learn here how to register a post type in WordPress.
// Is 'post' a registered post type?
$post_type_exists_post = post_type_exists('post');
// Is 'page' a registered post type?
$post_type_exists_page = post_type_exists('page');
// Is 'ns_book_cpt' a registered post type?
$post_type_exists_book = post_type_exists('ns_book_cpt');
Further reading
I recommend the other tutorials in this series to learn more about post types in WordPress.
- Post Types in WordPress
- How to Register a Custom Post Type in WordPress
- How to Register a Custom Post Type Using a Plugin in WordPress
- How to Unregister a Custom Post Type in WordPress
- How to Modify a Post Type in WordPress
- How to Change the Slug of a Post Type in WordPress
- How to Check if a Post Type Exists in WordPress
- How to Get the Registered Post Types in WordPress
- How to Get the Attributes of a Post Type in WordPress
- How to Get the URL of a Post Type Archive in WordPress
- How to Add Custom Post Types to the Main Query in WordPress
Source code
The source code developed in this tutorial is available here.
Comments