Welcome to the

WP CDA Meetup

https://www.flickr.com/photos/fabianosouza/5328797863/in/photostream/

In the News

Industry

  • Pantheon now runs on Google Cloud
  • Service workers in Safari
  • WP Engine Investment - $250M

In the News

Plugins

In the News

Core

Custom Post Types

Mike Selander / @Mike_Selander

  1. What Is a Post Type?
  2. When Should I Use CPTs?
  3. How Do I Make a CPT?
  4. What Can I Do with a CPT?

What Is a Post Type?

A Custom Post Type (CPT) is a Bucket for Content

WordPress Comes with CPTs

Posts

Pages

CPTs are what take WordPress from a blogging platform to a Content Management System

When Should I Use a CPT?

  • Does the content fit into Post or Page paradigms?
  • Does The content need a custom display?
  • Does the content need unique data?

Examples

Products

Doesn't fit Post or Page

Products

Needs Custom display

Products

Needs Unique Data

Galleries

Doesn't fit Post or Page

Galleries

Needs Custom display

Galleries

Needs Unique Data

How Do I Make a CPT?

Let's Make some Testimonials

One common function

register_post_type( $slug, $arguments )

Hook into init

add_action( 'init', function() {
	register_post_type( $slug, $arguments );
} )

Name our CPT

add_action( 'init', function() {
	register_post_type( 'testimonial', [ 'public' => true ] );
} )

Add some options

add_action( 'init', function() {
	$arguments = [
		'public'       => true,
		'labels'       => [ 'name' => 'Testimonials' ... ],
		'archive'      => [ 'slug' => 'testimonials' ],
		'menu_icon'    => 'dashicons-testimonial',
		'supports'     => [ 'title', 'editor', 'thumbnail' ],
		'show_in_rest' => true,
		'rest_base'    => 'testimonials',
	];
	register_post_type( 'testimonial', $arguments );
} );

Custom Admin Labels

$arguments = [
	'labels' => [
		'name'          => 'Testimonials',
		'singular_name' => 'Testimonial'
		...
	],
];

Custom Icon

$arguments = [
	'menu_icon' => 'dashicons-testimonial',
];

Admin Field Support

$arguments = [
	'supports' => [
			'title',
			'editor',
			'author',
			'thumbnail',
			'excerpt',
			'comments',
			'revisions',
	],
];

Admin Field Support

$arguments = [
	'supports' => [
			'title',
			'editor',
			'revisions',
	],
];

Public Support

$arguments = [
	'public' => true, // Available everywhere
];

Archive URLs

$arguments = [
	'archive' => false,
];
$arguments = [
	'archive' => [ 'slug' => 'testimonials' ],
];

Enable REST Support

$arguments = [
	'show_in_rest' => true,
];

What Can I Do with a CPT?

Display a Custom Template

single-testimonial.php
archive-testimonial.php

Query Just for Your Post Type

$posts = new WP_Query( [
	'post_type' => 'testimonial',
] );

Fetch Posts with the REST API

curl -X {args} https://my-site.com/wp-json/wp/v2/testimonials

Add Meta Boxes

add_meta_box(
	'meta-box-id',
	'My Meta Box',
	'my_display_function',
	'testimonial' // <-- display on testimonials
);

Thank you!

Have any questions?

Thanks for coming!