How to get post type object

As a WordPress developer, its very rare that you don’t know what is custom post type or post type. During WordPress theme or plugin development, sometimes it’s needed to check the slug of a registered post type or taxonomy slug of a taxonomy.

Here is a quick way, how you can check what is the slug of any build in post type of WordPress.

<?php 
            $args = array(
                'public'   => true,
                '_builtin' => true
             );
             $output = 'names'; // names or objects, note names is the default
             $operator = 'and'; // 'and' or 'or'
             $post_types = get_post_types( $args, $output, $operator ); 
             foreach ( $post_types  as $post_type ) {
                echo '<pre>';
                var_dump($post_type);
                var_dump(get_post_type_object($post_type));
                echo '</pre>';
             }
            
            ?>

Above code will give you the object of all built in post types. As you can see, _builtin parameter value is set to true inside argument array. get_post_type_object() function returns the full object of a post type.

<?php 
            $args = array(
                'public'   => true,
                '_builtin' => true
             );
             $output = 'names'; // names or objects, note names is the default
             $operator = 'and'; // 'and' or 'or'
             $post_types = get_post_types( $args, $output, $operator ); 
             
             foreach ( $post_types  as $post_type ) {
                echo '<pre>';
                    var_dump(get_object_taxonomies( $post_type, 'objects' ));
                echo '</pre>';
             }
            
            ?>

Above code will give you the object of all register taxonomy of built in post types. get_object_taxonomies() return the taxonomy object of a register post type.

<?php 
            $args = array(
                'public'   => true,
                '_builtin' => false
             );
             $output = 'names'; // names or objects, note names is the default
             $operator = 'and'; // 'and' or 'or'
             $post_types = get_post_types( $args, $output, $operator ); 
             
             foreach ( $post_types  as $post_type ) {
                echo '<pre>';
                var_dump(get_post_type_object($post_type));
                echo '</pre>';
             }
            
            ?>

Above code will give you the object of all register custom post types. As you can see I set the value of _builtin as false inside arguments array.

<?php 
            $args = array(
                'public'   => true,
                '_builtin' => false
             );
             $output = 'names'; // names or objects, note names is the default
             $operator = 'and'; // 'and' or 'or'
             $post_types = get_post_types( $args, $output, $operator ); 
             
             foreach ( $post_types  as $post_type ) {
                echo '<pre>';
                    var_dump(get_object_taxonomies( $post_type, 'objects' ));
                echo '</pre>';
             }
            
            ?>

Above code will give you the object of all register taxonomy of custom post type.