5 Things You Didn’t Know About Post Types in WordPress

Today WordPress is the most used Content Management System (CMS) around the globe. It supports websites, blogs, news channels, forums, social networks, and it’s available for everyone to go ahead and create their own web platform. But when WordPress started, about 12 years ago, it was used to create merely blog. As WordPress has progressed a plenty since, there are many more uses that are growing as time goes.

 

What is a post, anyway?

Think about the “Posts” section you know from WordPress. It’s a collection of articles, and each article has a set of common fields. For instance: title, content, categories, tags, link, etc.

These articles are part of the collection that’s called “Posts”. This collection, and what it means for the articles inside it, is what in WordPress you might call a Post Type. Besides the Post Types that WordPress has built in, you can create Custom Post Types (commonly referred to as CPTs) for your own website.

 

posts

 

5 things you didn’t know about Post Types

  1. We have them since 2005:
    Yup, Post Types have been serving us for a very long time. Technically, the use of Post Types in WordPress was first documented in version 1.5, when the field “post_type” was added to the database. However, the ability to register post types and use them within the admin panel was only possible since version 2.9, when the register_post_type was first introduced.
  2. WordPress actually has 5 built-in Post Types:
    Despite what people think, “posts” isn’t the only Post Type that’s included in the default WordPress installation. In fact, it includes 5 (!) of them: Posts (well, duh), Pages, Attachments, Revisions, and Menu Items. The reason all these WordPress features are made with Post Types is simply because a Post Type can be manipulated, updated, and read in very easy and accessible ways — like we’re about to see soon.
  3. By default, Custom Post Types have archive pages:
    When you create a Custom Post Type (as we’ll show you later in this article), you don’t need to get your hands dirty with WP_Query or any template files to display the posts under this CPT. By default, when a Custom Post Type is registered, you can view the posts within it by heading over to http://wordpressinstallation.path/post_type/. This URL path (where post_type is the CPT’s singular name version) will use the default post archive template to display the CPT posts.
    If you’d want to display the Custom Post Type in a different way, you can create a new template file called archive-post_type.php inside the active theme’s root directory and use the regular WordPress loop to walk through the posts.
  4. All the different labels of a Post Type are customizable:
    In the WordPress interface, WordPress will refer to your CPT in many different ways: in the side menu, in the archive pages, when searching for posts and more. All of those are easily customizable and translatable. For example, the “attachment” Post Type that’s built into WordPress is called “Media” in the sidebar, but the link to view the attachment is called “View Attachment Page”.
  5. Post Types don’t have to support all the post features:
    You can create a Custom Post Type that doesn’t support comments, thumbnails, or even post content. Enable only the features you need to keep the editing screen as lean as possible and to not confuse your content editors.

 

How do I create a Custom Post Type?

There are many plugins for doing that with a nice UI, but the best and safest way to make such changes in your WordPress website are by diving a little into the code. Using a plugin slows down your site, adds clutter to your dashboard, and saves unnecessary data in your Database. Don’t be tempted to use plugins. Don’t be afraid of touching a bit of code, it’s much easier than it sounds.

I already mentioned the basic function that allows us to add a Custom Post Type. It’s called register_post_type, and it accepts as parameters the post type’s name and an associative array of additional properties.

For example, here’s the basic code needed to create a simple CPT for videos:

register_post_type( ‘video’, array( ‘supports’ => array( ‘title’, ‘author’, ‘thumbnail’, ‘excerpt’, ‘custom-fields’ ) ) );

This is the very basic usage of the function. Looks a bit complicated? Don’t worry! We got you covered with a nice and easy interface to help you generate the right code for you, over at our Post Type Generator.

Here’s how to use it:

 

1. In the link, go to the “General” tab and fill in the form.

  • Function name could be pretty much anything, but it is a best practice to make sure to include unique info (like your theme’s name and the new CPT’s name) to make sure the function’s name will be unique. A simple example would be: yourname_projectname_post_types.
  • Turn on Child themes if you’re working on a Child Theme.
  • The Text Domain is the keyword that identifies your strings when translating text to other languages. If you’re not translating your website yet, you can just put the website’s name here.

 

general

 

2. Now, go to the “Post Type” tab and fill up the different fields in this section.

  • Post Type Key is the real name of the CPT. It’s one word, in English, that represents the Post Type (e.g. post, video, page, attachment, review…). This also has to be unique, and should ideally be in lower-case letters and singular (“video” not “videos”, “project” not “projects”). If the CPT’s name consists of a few words, then an underscore should be used as a separator (“vendor_product”).
  • The Name (singular and plural) and the description are simply labels. Users will see this, so make sure it’s clear.
  • Link to Taxonomies is a list of all taxonomies that will work with this Post Type. Taxonomies are like filters for posts, categories and tags are examples of taxonomies. In the example below, I want our Post Type to only have categories and not tags.
  • Hierarchical tells WordPress whether this CPT can have parent posts and child posts, just as in pages.

 

post_type

 

3. In the Labels tab, simply set labels for your CPT.

Like I mentioned earlier, it’s all customizable and sometimes it makes sense to use different labels. For example, to say “View Attachment Page” in the link to the attachment page, but to call it “Media Items” in other places (like WordPress does). Sometimes the thumbnail has a different name (see in the example below).

 

labels

 

4. Options

In the Options tab, make sure to select on the left only the features that are relevant to your Post Type, like I mentioned earlier. The Enable Archives field allows you to change the default archive page URL (the one listed in “Things you didn’t know about Post Types” #3).

 

options

 

5. Visibility is about where the Post Type gets displayed in the dashboard.

You can change the position of the Post Type in the side menu of the dashboard, or even cooler — set a custom icon for your CPT. I went to the dashicons page of WordPress and chose “format-video” to be my icon.

 

visibility

 

6. Query (Optional; Advanced)

If you want to alter the way WP_Query is being used to get posts from your CPT, select the Query tab. This one has a few nice settings to help you with using WP_Query, or to make sure the CPT isn’t publicly queryable (accessible). I’d just leave it as it is.

7. Permalinks (Optional)

The Permalinks tab is the place to change some archive page settings, like disabling pagination or changing the URL of the archive page. Again, I would just keep it the way it is.

8. Capabilities (Optional)

Capabilities is all about who can manipulate posts under a CPT. You can make sure that only editors or administrators can change, add, or delete posts under your CPT. This becomes super useful when you have a lot of editors in the site and you want to make sure no one accidentally deletes or messes up anything.

9. Update Code

We’re almost done! Now, click on the Update Code button.

 

 

That’s it! The generated code is ready for adding this CPT to your WordPress website. Now it’s time to actually put that code in your current theme’s functions.php file.

In your favorite code editor, open the functions.php file from the current theme folder.

post_types_functionsphp_file

Now, go back to the generator, copy the long code in the grey box and paste it at the bottom of the same functions.php file.

post_type_functionsphp_after_paste

 

Make sure to save the file, and that’s it, you’re done! You can now go to the new Videos page from the navigation pane, and add new Videos!

 

cpt

 

Thanks for reading! I hope you enjoyed this basic guide to Post Types. If you liked the post, please leave a comment and let me know!

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on whatsapp
Share on email
Reuven Karasik

Reuven Karasik

Web Developer and young entrepreneur.

3 Comments:

  • Profi-Blog.com - Rudolf Fiedler

    Hi reuven,
    thanks a lot for your cpt-generator, it’s easy and works perferkt.
    But – why put the code into the functions php ot the theme?
    CPT’s are often a specialt type with special functions, and mostly it would be better situated in a plugin.
    Whats the reson(s) for you to include the code in the theme, and not in a plugin?
    Thanks a lot,
    Rudi

    Reply

    • Dan Knauss (@newlocalmedia)

      My thought exactly! Make your CPTs portable; put them in a plugin.

      Reply

    • Reuven Karasik

      Hey guys!

      Generally speaking, you’re obviously right. Putting your Custom Post Types inside a plugin is definitely better than putting it in your theme, but for the purposes of this quick tutorial, I wanted to make the process as simple as possible, so I put the code in the functions file (creating a plugin takes a few more steps which we might elaborate on in a different post sometime).

      Thanks for your comment though, I will add a note to the post.

      Reply

Leave a Reply

Subscribe to our Blog

Receive email notifications when new blog posts are published.