Custom Post Types

Jon Bishop
June 25, 2012

What Are Custom Post Types

When Should I Use Them?

Anywhere you need a new content type

Creating New Post Types

add_action( 'init', 'create_post_type' );
function create_post_type() {
	register_post_type( 'my-post-type',
			'labels' => array(
				'name' => __( 'My Post Type' ),
				'singular_name' => __( 'My Post Type' )
		'public' => true,
		'has_archive' => true,

Querying by Post Type

$args = array( 'post_type' => 'my-post-type', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
	echo '<div class="entry-content">';
	echo '</div>';

Using Custom Post Types

Getting the post type of a post

$post_type = get_post_type( $post_id );

Checking if a post type exists

if ( is_post_type( 'my-post-type' ) )

Modifying Custom Post Types

Remove support of certain features for a given post type

add_action('init', 'my_custom_init');
function my_custom_init() {
	add_post_type_support( 'my-post-type', 'author' );
        remove_post_type_support( 'my-post-type', 'excerpt' );

Checks a post type’s support for a given feature.

post_type_supports( 'my-post-type', 'editor' )

Custom Post Type Templates

Custom Post Types display

Custom Taxonomies display

Stuff To Remember

Execute flush_rewrite_rules with register_activation_hook to make pemerlinks work with your new post type

Prefix your name with a short “namespace” so you don’t conflict with other theme’s and plugins


Jon Bishop
Web Developer @ AMP Agency

Website /
Twitter / @JonDBishop