Ultimate WordPress Custom Post Type Tricks Roundup

More often than not my day consists of WordPress website problems I need to find a solutions for. Much of it I can draw from my own experience, but often I’ll Google for the answers I need. Usually though, the answers aren’t found just in one place – they come from forums, blog comments, tutorials, and Q&A sites combined. Over time I gather tidbits of notes on common subjects, and that’s how blog posts like this one are born.

I posted a lengthy blog post yesterday on my blog (WordPress custom post types on steroids), in which I described how to create and manage custom post types using both PHP code and plugins. While writing that post, and while doing client work this week, I came across some really unique custom post type plugins and tricks. Being a big fan of “roundup” resource posts, I’ll present you with all kinds of custom post type goodies you can use in your WordPress website today.

Issues with Custom Post Types

Know your code…

Custom post types in WordPress are great, they turn it from merely a blogging system into a fully fledged CMS with grown up taxonomy capabilities. The problem with them is, you can’t “just create one” in your wp-admin dashboard (like you can a page, post, tag, category, etc.). Most developers I’ve asked use PHP code to create and maintain their custom post types, but some use plugins.

If you are using code, one really cool thing we’ve run across is this WordPress Custom Post Type Code Generator page.

Your mileage may vary with plugins…

Since there are so many options available for custom post types, your mileage with plugins may vary. In most plugins it’s easy to create a new post type, but it varies for editing or deleting. Most don’t handle any type of “migration” at all (renaming a post type, or moving posts from one post type to another). Some have “export” capabilities, some don’t – some CPT plugins place with others (some don’t). There are also different options for dealing with custom fields and meta boxes.

Getting the content to display…

Whether you get a custom post type created by PHP code or a plugin, once you get some content in it – the problem moves from how to create them to how to display them.

The initial issue is the home or archive page for the custom posts. Then you might want to customize the individual single custom post pages, and you might have to deal with the display of additional custom fields or meta information. Last are the style and layout concerns for the post content.

Fixing Common Custom Post Type Problems

We’re going to run through some common custom post type issues we’ve faced, along with some resources we’ve come across for fixing them.

It would probably be helpful first to explain the “template file hierarchy” for custom post types. We’ll explain more about them as we go.

archive-{post_type}.php
single-{post_type}-{slug}.php

How Do You Display Custom Post Type Archives?

In WordPress a page with a list of posts is commonly known as an “archive” page. Your blog page with latest posts, category and tag pages, date based post lists – are all “archive” pages. If you have a custom post type created, more than likely you’ll some type of inner page to display them in list view (to get at the single page for each).

When custom post types first appeared in WP, you had to create your own archives page manually, which is when plugins like Simple Custom Post Type Archives and Custom post type archives started coming out.

Then (in WP 3.1) archive support for custom post types came out, as long as the CPT was created with the “has_archive” parameter set.

Here’s the PHP code for that:

add_action( 'init', 'sample_init' );
 
function sample_init() {
 
    register_post_type( 'example', array(
        'labels' => array(
        'name' => __('Examples'),
        'singular_name' => __('Example')
            ),
        'public' => true,
        'show_ui' => true,
        'rewrite' => array(
        'slug' => 'example',
        'with_front' => false
            ),
        'has_archive' => true
    ) );
}

Before that functionality existed, WordPress developers started the process of creating an archive by creating a new “Page” in the WordPress dashboard. Let’s say your page is for a new custom post type “products”, the page would likely be called “Products” as well.

Next download “page.php” from your WP theme folder in FTP and “save as” something like “page-products.php”.

Then add this code to the top of the file, save – and re-upload to your theme folder:

/*
Template Name: PostTypeProducts
*/

Then reload the edit screen in wp-admin for your “Recipes” page and you would switch to your new template page for that custom post type, as in this example:

custom-post-type-template-page

Last – you need to update “the_loop” in page-products.php to display your new custom post type archive. You can find that code if you need in the WP Beginner post about creating a custom post type archive page.

Even though there’s a new way to do this now (which we’re about to explain), let’s talk about when you would use this “theme page template” method for a custom post type archive page. When you have a client that wants editable text above the CPT archive (that they can edit in wp-admin from the page edit screen), this page template is definitely the way to go.

Now that you know the page template way to do it – let’s look at the theme template hierarchy way. If your CPT has the “has_archive” parameter set to true as we previously showed, the your custom post type can use the normal WordPress theme template hierarchy.

So if your custom post type is “products” – then by default with has_archive set to true your website should show www.website.com/products as an archive of products posts using “archive.php” as the theme template for display.

For a custom page this would be the template structure:
archive-{post_type}.php

So download archive.php and “save as” archive-products.php, and customize it to your liking, and re-upload. This theme template page will now be used for www.website.com/products only.

How Can You Modify Single Custom Post Type Page Layout?

Just like “single.php” would control layout of normal posts, you can create a new template for custom post type single pages (if you want a different layout). You’ll definitely need to do this if you’re going to display custom fields or meta information associated with your CPT content.

This is the format to use:
single-{post_type}-{slug}.php

So if we wanted to create a single template page for products we could download single.php and “save as” single-products.php. This theme file would then be used for all products pages. However, let’s say we had a products post page with a special layout called “Deal of the Day”. Just like we would edit the page in wp-admin at “Products -> Deal of the Day”, to create a theme template for this page we would name it single-products-deal-of-the-day.php, and it would be used for just that one single custom post.

Having explained both of those methods, now here are some quick and dirty custom post type display solutions with plugins:

First, the Custom post type list shortcode plugin. This little gem will output a list of custom posts wherever you can put a shortcode. You could use it in the content area of any post or page, or even in a widget – a great solution for a client or non-coder that needs to get this done easily.

custom-post-type-selector

Second, you might be interested in the Post Types Order plugin, which allows you to quickly and easily order any post types in a drag and drop sortable manner without having to delve into the code of individual theme template pages.

sortable-custom-post-type-order

How Do I Assign a Page Template to a Single Custom Post Type Page?

Even though we just showed how to easily create a single custom post theme template file, there are reasons why you might want to choose another route. In WordPress, with a normal static “Page”, you can assign a “page template” in the page attributes of the edit screen like this:

wordpress-page-attributes

You can’t assign a page template to a normal “post” (because they’re for pages). Using the Custom post template plugin you can create single custom post templates, and select them from page attributes on the edit screen.

You might think – why would I need this plugin if I can create them template pages using the single-{post_type}-{slug}.php format for custom posts? The reason is, that’s a great way if your a coder and regularly use FTP. But if you have clients and multiple custom post templates they might need to select – this is clearly the better way (for that scenario).

Making Custom Post Types Display in Normal Archives

The “archives.php” is by default the homepage for most archives (date based). In some themes if there is no tag.php or category.php specific pages, the archives.php file runs tag and category archives as well.

Be default archives.php would show normal “posts” and not custom post (types). You can however, make archives.php include custom post types easily with this code:

function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'your-custom-post-type-here'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

How To Make Custom Post Type Taxonomy Template Pages

It’s easy to get very confused with WordPress custom posts types and taxonomy. A taxonomy is an ‘ordered list of things’. A taxonomy can exist without a custom post type. A taxonomy can be associated with one or many post types. Categories and tags, for example, could be associated with both normal posts and a custom post type. You can share a taxonomy among posts (or not). You could have a CPT for both “homes” and “apartments”, and both could share a taxonomy “location”. Or they could instead have their own independent “location” taxonomy.

In a bare WordPress install classic posts have built in taxonomy for both categories and tags. The template hierarchy allows you to create theme template pages for theme like this:

archives.php
category.php
category-{category}.php or category-{id}.php
tag.php
tag-{tag}.php or tag-{id}.php

The theme will use archives.php by default for tag and category display, but if you have a tag.php or category.php it will use those (if available). You can create individual cat or tag pages using the -slug versions if you need to. You can’t use this method to create theme template pages for new custom post type taxonomy you’ve created though.

You need to instead create a “taxonomy.php” file in your theme folder. Much like category.php or tag.php, taxonomy.php will be the default file used for any custom taxonomy item.

Here’s the template hierarchy for taxonomy items in WP:
taxonomy.php
taxonomy-{taxonomy}.php
taxonomy-{taxonomy}-{slug}.php

So our custom “location” taxonomy would be controlled by this page:
taxonomy-location.php

If we wanted to change the layout for our headquarters page in Dallas then the page would likely be:
taxonomy-location-dallas.php

How to Display Multiple Custom Post Types on One Page

There’s a lot of ways to do this, but here’s some code that displays all public custom post types (thanks Pippin!):

// grab all public post types
$post_types = get_post_types( array('public' => true), 'names' );
 
// loops through each post type
foreach( $post_types as $type ) {
 
// setup the query
$query_args = array(
'post_type' => $type,
'posts_per_page' => 1
);
 
// perform the query
$items = get_posts( $query_args );
 
// check if we have found anything
if( $items ) {
 
// loop through the items
foreach( $items as $item ) {
 
// show the post thumbnail
echo get_the_post_thumbnail( $item->ID, 'thumbnail' );
 
}

How to Add Custom Post Types into WordPress Navigation Menus

You might find after creating a CPT that when you go to Appearance -> Menus you can’t find the content to select. You need to make sure the custom post type has show_in_nav_menus set to true. Also in the top of the screen under “screen options”, make sure your custom post type is checked (so you can see it).

Also, if you need to add your main CPT archive pages into your WP nav menus try using a plugin like Custom Post Type’s Archive in WP Nav Menu.

If you need to add custom post links as children of a custom post type archive in navigation – you might try the Custom post types sub menu plugin.

How to Make RSS Feeds for Custom Post Types?

You don’t have to, they are already built into WordPress.

You can access RSS feeds for custom post types like this:

http://www.website.com/feed/?post_type=products

or taxonomy items like this:

http://www.website.com/feed/?post_type=products&type=books

However, just because you can access the RSS feeds at these URL’s doesn’t mean they are added into the header of your theme (like they should be). You can get that done automatically using the Custom Post Type RSS Feeds plugin.

How Do I Make ‘Show Custom Post Types’ on my Homepage?

Maybe, despite the fact that you have a custom post type archive – you want to show that content on your homepage (instead of classic posts).

You could add some code like this to your functions.php file of your theme:

add_filter('pre_get_posts', 'my_get_posts');
function my_get_posts($query){
global $wp_the_query;
if(is_front_page()&&$wp_the_query===$query){
$query->set('post_type','products');
$query->set('posts_per_page',-1);
$query->set('post_status', 'public');
}
return $query;
}

Another solution might be to use the Custom Post Type Selector plugin, which enables a new selection under “Settings -> Post Types”, where you can choose which post types are selected for inclusion in the main loop of your blog. By doing this you could intermingle custom post types with classic posts, or turn them off in favor of custom post types, or even use multiple custom post types – all without custom code or changing/creating theme templates.

custom-post-type-selector

How to Migration Content From One Custom Post Type to Another

So you have a client that has a custom post type called “products” and they want to rename it to “goods”. Easier said than done right? If you just rename the custom post type – all the content will be lost (no longer associated with it). You could do some db code magic to fix this, or use the Custom Post Type Switcher plugin to switch the post types. You can do the same thing with Post Type Converter.

Caveat: (currently) can only switch from the post edit screens (one by one), and you have to create a new post type to move them to, then you can delete the old one safely.

If you need to do this in bulk – then try either Conver Post Types, or Post Type Convertr.

How Can I Edit Custom Post Type Permalinks?

Check the advanced section below in resource for an in-depth tutorial on how this works, or choose an easier route like using the Custom Post Type Permalinks plugin. You might also have a look at the Permalinks for Hierarchical Custom Post Types if that situation applies to you.

Sometimes you might not want to edit the permalink, as much as you’d like to remove the slug so the URL’s for custom posts are more in the /%postname%/ format (like www.website.com/%custom_post_type_title%/). The remove slug from custom post type plugin gets it done.

Custom Post Type Resource Links

Specific Use Custom Post Type Tutorials

Other Custom Post Type Plugins Not Mentioned Already

Types Custom Post Type Plugin
Types is one of the most fully featured and supported custom post type creation and management plugins available for free.

Latest Custom Post Type Updates
Basically a “recent posts” widget you can use with any custom post type, awesome!

Custom Post Content Display Widget
This is a cool little plugin that will display the contents of a single custom post in a widget.

WordPress Custom Post Type Archive
A monthly or yearly archive widget for custom post types.

Custom Post Types Tree
A plugin where you can setup parent / child relationships between different custom post types.

Gravity Forms + Custom Post Types
Create a gravity form that creates custom posts. You can map the form fields to your custom taxonomy – works great if you have a site with the need for people to submit (custom post) content from the front end!

Custom Content Type Manager
Another plugin to create and manage custom post types and custom fields.

Custom Search Plugin
This plugin has a feature that allows you to include custom post type content in WordPress search results.

Automatic Featured Image Posts
We mentioned this plugin because it has the ability to choose any custom post type in your WP website to have automatic featured image posts (without having to code it into your theme pages).

Theme Specific Custom Post Type Resources

Advanced Custom Post Type Topics

Creating a Tabbed Widget for Custom Post Types
Unique tutorial showing how to create a really cool tabbed widget to display custom post types.

Add Custom Post Types to Author Archive Page
If you have a website with multiple authors (like this one), by default the authors archive page shows just normal posts that they have written. This post shows how to show custom post types they’ve written as well.

The Rewrite API for Custom Post Types and Taxonomies
This one is a good read – as you’ll learn how custom post types don’t use the normal permalink settings, and how to setup your own URL structure (if you need to).

How to List Terms of a Custom Taxonomy
This is very helpful if you need to list taxonomy terms on a page the same way you would categories or tags.

Custom Post Type Action Hook
Learn how to create an action that runs every time a particular custom post type is published.

How to Query Multiple Taxonomies
Shows how to use the query_posts parameter tax_query to query multiple taxonomies.

Adding Custom Columns in Custom Post Types Admin
Great tutorial on how to customize your custom post type management and sorting screen.

How to Use Radio Buttons with Taxonomies
Unique tutorial showing how to use radio buttons with taxonomies, vs. the input box method.

Custom Post Type Pagination
If you were looking for a solid pagination custom post type tutorial that doesn’t involved wp-pagenavi, this is it.

Adding Post Type Archive Links to your Nav Menu
Learn how to get your custom post type archive pages to show in nav menus without having to add them manually.

Building Your Own Custom Post Type Plugin

Want to build your own plugin for managing custom post types? This post has all the code you need to get it going. If you have any more tips and tricks please share them in the comments below. I would love to hear from you.

This article was authored by:

John Pratt is principal WordPress developer at JTPratt Media, where you can find him working on custom themes and plugins for small business and corporate clients.

John Pratt has authored 12 posts.Visit Website

Showing 8 Comments

  • Wow – thanks for a really comprehensive write up! I’ve bookmarked it for reference. :)

    I recently started using the Gravity Forms + Custom Post Types and love it. I’ve also been playing with the Types and Views plugins for easy CPT & custom field creation (Types) and quick ways to display (Views). Have you used Types & Views or have an opinion on it?

    REPLY
  • John Pratt (Admin)

    @Carrie – I like both Types and Views, and in the first link to my last blog post, I write about both of those plugins. When I don’t use code, I use those (now), and I’m starting to use views more and more – because it has applications even outside of custom post types. It just plain makes it easier to build specific customality quickly without coding.

    REPLY
  • Hey nice way of sharing about it. Keep Sharing… Thanks!

    REPLY
  • Sudhakar

    This is the best post I have come across on the custom post types. Great job !!

    REPLY
  • Hello, thanks for this awesome article!

    There’s one thing I’m still looking for though, it’s how to handle where the media/images from custom post types go .. at least decide if they go with the regular media folders or to another kind.

    can’t find any plugin so far…

    have a nice day
    sophie

    REPLY
  • I have a problem with the archives of the custom post type.

    Changes to the file archive and the new post-customPost.php are only visible to admin mode while the normal user does not see the new posts and the changes made ​​to the code.

    Could you help me?

    REPLY
  • I’ve been searching for a week for a solution on how to set a static page as the parent of a Custom Post Type page. Any insight? Here’s a description of my situation and what I’m trying to achieve: http://wordpress.org/support/topic/how-to-set-static-page-as-parent-of-custom-post-type-page

    REPLY
  • Hi,

    Good read. I’d like to add that whilst a plugin can handle the creation of Custom Post Types, many (for a number of reasons) prefer to code the integration.

    I’ve produced a post detailing the steps for linking Gravity Forms with a custom post type, if you care for it:
    https://bay-a.co.uk/wordpress-tips/gravity-forms-custom-post-from-form/

    Also, to answer @Sophie’s question above, they go into a specific folder for gravity forms uploads by default -> wp-content/uploads/gravity_forms/

    Thanks,
    Lloyd

    REPLY

Add Your Voice: