A short talk on Custom Post Types in Wordpress. Presented at the March 16 Wordpress Edmonton meetup at Startup Edmonton.
Get the companion plugin on my github here:
https://github.com/StephenBrownski/wpyeg_custom_post_types
2. Who’s this guy?
● Stephen Brown
● Graduated from DMIT at NAIT
● Web Developer at AgileStyle
(agilestyle.com)
● Primarily work with Wordpress and Front-End languages.
● Dipping my toes into Ruby and Ruby on Rails
3. Before we begin!
Thank You!
I made a plugin!
https://github.com/StephenBrownski/wpyeg_custom_post_types/
4. Why Custom Post Types?
● Was semi-volunteered...
● Most of us use them, and have probably
used a generator of one kind or another.
But what are they really?
It’s your job as a developer to understand
the code you are using, especially if you
didn’t write it.
5. Post types trivia
● First introduced in 2005 with Wordpress 1.5 when ‘pages’ were added.
● register_post_type() - the focus of this talk - was added to core in
Wordpress 3.0
● Much of Wordpress’ core content is made of distinct post types:
o Posts, Pages, Attachments, Revisions, and Navigation Menus
6. Why use them?
● They help you get past limitations of the default types (eventually).
● Custom Post Types allow you to determine your own dynamic content
types.
● Let you avoid fumbling with html structure in your post content.
Post types are a part of what makes
Wordpress so flexible
7. From the codex:
$post_type: (string) The handle for the post type.
- No capitals letters or spaces
$args: (array) The options for the post type.
- Reference ‘post_types.php’ in the plugin for these options.
Down to business!
register_post_type( $post_type, $args );
8. List of all posts of this post type: (Assuming ‘publicly_queryable’ is true)
Go to single post:
Template Files:
Archive - archive-post_type.php
Single Post - single-post_type.php
Now what?
example.com/?post_type=post-type-slug
example.com/post-type-slug if rewriting set
example.com/?post-type-slug=single-post-slug
example.com/post-type-slug/single-post-slug if rewriting set
9. The next step
Unfortunately, this is only part of the custom content puzzle.
Still need:
● Custom Fields
● Custom Taxonomies
Resources
Smashing Magazine - Guide to CPTs
BetterWP - Custom Post Types Explained
Themergency - Post Type Generator
Wordpress Codex - register_post_type()
Who’s this guy? (That is not a trick question...)
My name is Stephen Brown. I graduated Digital Media and IT at NAIT, and have been working in industry at AgileStyle for almost two years.
I primarily work in front end and wordpress development, though I am learning Ruby on Rails as I go.
Thank you to the event organizers and everyone for coming out tonight.
I have made a sort of companion plugin. Installing the plugin will create three custom post types and populate them with a small amount of sample data. You can check it out after the talk.
So, why am I doing a talk on custom post types?
Well, for starters, I was sort of volunteered. But beyond that, custom post types are a thing that almost every wordpress developer works with, and it’s usually in the form of using a code generator or plugin and then crossing your fingers.
This is not enough! It is your job as a developer to understand at least the basics of the tools and the code you are using. Especially so if you didn’t write it.
First, a little trivia!
Post types were first introduced to wordpress - albeit in a very primitive form - in 2005 when support for static pages was added. This added the ‘post_type’ column to the posts table. At that time, there was only posts and pages.
Register_post_type(), the focus of this talk, was released to the public with Wordpress 3.0.
Finally, much of wordpress’ core content is made of distinct post types, and are all stored in the ‘posts’ table. These are posts, pages, attachments, revisions, and even navigation menus.
So why would you want to use a custom post type?
First, they help you get past some of the built in limitations of pages and posts. As handy as they are, you can only do so much with them.
CPTs also allow you to determine your own dynamic content types. You can also set your own rules for each of these. Want a post type that will only reside in other pages templates, without having a single view of its own? You can do this!
And perhaps the most important point, is that custom post types allow you to avoid fumbling around with html structure in your wysiwyg editors. I’m sure many of you have worked on a page in wordpress where you end up copy/pasting repeating content divs into your html editor. You can completely bypass that by defining that repeating content type as a custom post type and looping through it in either a page template or a shortcode.
Business time!
The function itself is very simple on the surface, and only accepts two arguments. One: the post type slug. Note: You cannot have capital letters or spaces here. The second, is an array of arguments. That array will hold all of the options and settings that make your custom post type unique. To go over this, I am going to reference one of the post type declarations in the plugin I have put together.
Now that you have this brand spanking new post type, what can you do with it?
To access a list of all the posts of this type, you can navigate to /?post_type=post-type-slug, or /post-type-slug with permalinks enabled.
To access single posts of this type, you can navigate to /?post-type-slug=single-post-slug, or /post-type-slug/single-post-slug
Finally, the template files you will need for this post type are archive-post_type.php and single-post_type.php
Unfortunately, custom post types are only one piece of true custom content on wordpress.
For complete control, you will need to leverage custom fields and custom taxonomies, which are each a full talk of their own.
Lastly, here are a few helpful resources for some further reading on custom post types in wordpress.
So that wraps it up for my brief overview of custom post types. Are there any questions?