Lunch & Learn: Custom Post Types

Posted December 10, 2014 by   in The Springboard Culture, Web Development, Wordpress Admin

Every second Wednesday, the Springboard Family dedicates our weekly team lunch to learning from one of our family members. We like to call it a lunch & learn and this week’s topic was, “Custom Post Types” taught by me.

What are Custom Post Types?

Custom post types are a post type that is separate from Posts or Pages that allow different types of content to be distinguished on a website. A custom post type works exactly like the Posts of a WordPress website, with a main landing page and single posts pages, but is integrated into the website a little differently. Unlike Posts, however, custom post types are not default to the WordPress install and must be added by a developer.

Popular Custom Post Types:

FAQs, Testimonials, Case Studies, Resources

When/Where to Use CPTs?

If a website has more to it than just regular content pages and a blog, custom post types are almost necessary. If random testimonials need to be displayed each time the Homepage loads, a custom post type should be used. Basically, if you want to display a post type’s content in different spots other than it’s landing page, a custom post type should be utilized.

Why Use CPTs?

It makes it easier for the user to add content to their website because a custom post type can be made out of any type of content that you want to dynamically display on a website where there will be multiple posts of; for example, a Product custom post type could display multiple products on one landing page. It distinguishes all the different types of content from each other for easier editing.

How Are CPTs Used?

With a little bit of code added to the functions.php file on the backend, you can register new custom post types and they will show up as tabs in the WordPress Dashboard along with the Posts and Pages tabs. WordPress also offers free dashicon icons that you can add to the title in the Dashboard to make them look like they came “out of the box” with the WordPress install. By default, registering a custom post type doesn’t mean it has any fields, but in that same functions.php file, you can modify the code to allow the Page/Post Title, WYSIWYG editor and Featured Image.

Advanced Custom Fields Integration

Most custom post types require more than just the basic post page (except for maybe FAQs where the title is the question and the content editor is the answer), so this is where Advanced Custom Fields comes in. To add a new field, simply create a new group in the custom fields tab, create the amount and type of fields needed for the functionality of the custom post type, then you assign it to the post type so it shows up when you go to add/edit that specific post type.

For instance, for a Gallery post type, a gallery and a page intro (simple text area field) custom field might be added in tandem to the title and featured image (which could be used as the cover photo for the gallery wherever it’s displayed).

Displaying Content on Website

Although we’ve added the custom post types and custom fields for the custom post type, the information needs to be displayed on the actual website. To do this, a new landing page template must be created in the theme folder in the backend. Within that template, a WP_query that takes multiple arguments (what the post type is called, number of posts to display, what order to display the posts in, etc.) will be used to display the information. Within that query, you can access the information from the custom fields that were added to the post type and display them on the page. If the custom post type requires a single post page (some don’t; like Testimonials), another template needs to be created. This doesn’t use a query because it will just display the custom fields from the post that you clicked into.

Helpful Hint

If you have a Products custom post type, the landing page (that houses the query and displays the list of posts in that post type) for the custom post type will be called “page-products.php”. WordPress is also smart, so if you use the same slug structure (“single-products.php”), it will already know what post type to use in the single post page.