http://www.phparch.com \ December 2018 \ 3
Custom Post Types in WordPressFEATURE
Custom Post Types in WordPress
Andrea Roenning
Custom post types are the key to taking a WordPress website from a simple blog to
a robust system for managing many types of content and data. They help create a
WordPress administrator experience which makes it easy for editors to add and edit
repetitive content and gives the developer flexibility to create unique web pages to
fit the site’s individual needs.
Not that long ago, I was a WordPress
hater. On top of some bad experiences
with plugins and themes, I found the
editing tools to be limited and some-
times confusing to clients for whom
I was building sites. But working with
custom post types has opened my eyes
to the fact WordPress can be much
more than a blogging platform—it can
be a powerful content management
system (CMS) I can design myself with-
out needing a ton of third-party plugins.
I can customize the WordPress Admin
(WP Admin) editing experience to
make it easy for site managers to update
data in a way that fits their site’s needs.
Why Use Custom Post Types?
The Board of Directors Page
Dilemma
An excellent example of when to use a
custom post type is a page that displays
a grid of photos and names, such as a
board of directors page.
You could do this in WordPress out of
the box by writing some HTML inside
the editor. Perhaps you’d wrap a div
around each photo and name group
and give it a class. Then you would write
some CSS, either in your custom theme
or by adding it to the theme you’re
working in to help with layout. (Or
perhaps you’d use the Gutenberg editor
to create a column with this informa-
tion.) Then copy, paste, copy, paste.
Easy, right?
But, then, what if your client asks:
- Can you sort these alphabetically?
- Can you add three more people?
- Can you give each person a bio
page linking from their picture?
- Can we also list the board of direc-
tor’s names on this other page? - How can I edit someone’s name
without “breaking” the layout?
If you set up your photo grid page
using HTML within the WordPress
editor, all of these tasks can be time-con-
suming and require a web developer to
move around the HTML code if the
user isn’t comfortable making these
edits. And, if you manual repeat data
on another page, you risk the two sets
of data falling out of sync.
Custom Post Types to the Rescue!
Since we’re already using a CMS,
surely there’s a better way to manage
this content. The board of directors
page is a perfect example of when to use
a custom post type:
- Each listing shares a common set of
components (name, bio, photo). - You can leverage WordPress’
built-in tools to display this data
(the_title, the_content, thepost
thumbnail). - You can design a custom template
to query and filter based on this
data (for example, to sort alphabeti-
cally or only show people who have
a photo uploaded). - Links to individual bio pages for
each person can be easily added. - This data could be easily queried
and added to additional pages. - Your WP Admin users could easily
edit or remove existing members
of the board or add new ones using
the same WordPress tools they use
to writing posts and pages.
Custom post types are great for all
sorts of structured data. I’ve used them
for ad delivery, conference sessions,
ebooks, testimonials, calendar of
events—pretty much any time I find
myself wanting to copy and paste a
block of frontend code to repeat some
data in WordPress.
Using Custom Post Types
- First, create your custom post type.
2. Next, add some data to your Word-
Press admin panel.
3. Third, create a template to display
the data (or use the WordPress
fallback pages).
Using a Third Party Plugin
There are several WordPress plugins
which make it easy to create custom
post types.
The plugin CPTUI^1 provides an inter-
face for creating and managing your
custom post types. If you’re just getting
started with custom post types, explor-
ing this plugin’s options is an effective
way to learn about all of the WordPress
options for custom post types.
Registering Your Custom Post
Type In a Theme
With minimal knowledge of PHP and
the WordPress documentation, you can
create a custom post type in a custom
theme by adding code to your theme’s
functions.php file.
1 CPTUI:
https://phpa.me/custom-post-type-ui