WordPress

Custom post types in WordPress: when and how

Avi, the Avenux Techspire mascot, illustrating: Custom post types in WordPress: when and how

WordPress comes with posts and pages, and for a lot of sites that is enough. But sooner or later a client wants a portfolio, a team section, a list of properties or a course catalogue. You could dump all of these into regular posts with categories, but it gets messy quickly. That is where custom post types come in.

When a custom post type makes sense

A custom post type is a new kind of content with its own menu in the admin, its own URLs and its own templates. Use one when:

  • The content is clearly a different thing from blog posts. A project is not an article.
  • It needs its own fields, like a property price, location or number of bedrooms.
  • It needs its own listing page and its own single page layout.
  • Editors would get confused if it was mixed in with the blog.

Do not create one just to group posts. If the content is still basically articles, a category or tag is the simpler answer. A “News” section on a company blog is usually a category, not a post type.

Registering a post type

You register a post type with register_post_type, hooked on init. Here is a working example for a portfolio:

<?php
function mysite_register_project_type() {
    register_post_type( 'project', array(
        'labels' => array(
            'name'          => 'Projects',
            'singular_name' => 'Project',
            'add_new_item'  => 'Add New Project',
            'edit_item'     => 'Edit Project',
        ),
        'public'       => true,
        'has_archive'  => true,
        'rewrite'      => array( 'slug' => 'projects' ),
        'menu_icon'    => 'dashicons-portfolio',
        'supports'     => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'show_in_rest' => true,
    ) );
}
add_action( 'init', 'mysite_register_project_type' );

What the important options do:

  • public makes it visible on the front end and in the admin.
  • has_archive gives you a listing page, here at /projects/.
  • rewrite sets the URL slug. Single projects will live at /projects/project-name/.
  • supports controls which boxes appear in the editor. Add ‘custom-fields’ or ‘revisions’ if you need them.
  • show_in_rest turns on the block editor and the REST API for this type. Leave it out and you get the old classic editor screen.

The post type key (project here) should be short, lowercase, with no spaces, and no longer than 20 characters. Avoid names WordPress already uses, such as post, page or attachment.

Put it in a plugin, not the theme

This is the mistake I see most. If you register the post type in functions.php and the client later switches themes, all their projects seem to vanish from the admin. The data is still in the database, but nothing registers the type any more.

Content structure belongs in a plugin. A small site-specific plugin, or a file in wp-content/mu-plugins, is perfect for this. The theme should only decide how things look.

Fixing the 404 after registering

You register the type, add a project, click View, and get a 404. This is normal. WordPress caches its URL rules and does not know about your new slug yet.

The quick fix is to go to Settings, Permalinks and click Save Changes. That rebuilds the rules. If the post type lives in a plugin, do it on activation:

register_activation_hook( __FILE__, function () {
    mysite_register_project_type();
    flush_rewrite_rules();
} );

Do not call flush_rewrite_rules on every page load. It is slow and only needs to happen when the rules change.

Adding a custom taxonomy

Projects often need their own way of grouping, like project type. A custom taxonomy is like categories, but just for your post type.

function mysite_register_project_taxonomy() {
    register_taxonomy( 'project_type', 'project', array(
        'labels'            => array(
            'name'          => 'Project Types',
            'singular_name' => 'Project Type',
        ),
        'hierarchical'      => true,
        'public'            => true,
        'rewrite'           => array( 'slug' => 'project-type' ),
        'show_in_rest'      => true,
        'show_admin_column' => true,
    ) );
}
add_action( 'init', 'mysite_register_project_taxonomy' );

Set hierarchical to true for category-style terms with parents, or false for tag-style terms.

Templates for your post type

Thanks to the template hierarchy, you can add these files to your theme:

  • single-project.php for one project
  • archive-project.php for the /projects/ listing
  • taxonomy-project_type.php for each project type listing

If they do not exist, WordPress falls back to single.php, archive.php and so on.

Showing projects elsewhere on the site

To show the latest projects on the home page, use WP_Query:

<?php
$projects = new WP_Query( array(
    'post_type'      => 'project',
    'posts_per_page' => 6,
) );

if ( $projects->have_posts() ) :
    while ( $projects->have_posts() ) : $projects->the_post(); ?>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endwhile;
    wp_reset_postdata();
endif;
?>

Always call wp_reset_postdata() after a custom loop, otherwise template tags further down the page may show the wrong post.

Custom fields

Most post types need extra fields: a client name, a link, a price. You can add meta boxes with add_meta_box and save them with update_post_meta, or use a field plugin like Advanced Custom Fields if the project already uses it. Whichever way you go, sanitise input when saving and escape it when printing.

A quick checklist

  • Is this truly a different type of content? If not, use a category.
  • Register on init, inside a plugin or mu-plugin.
  • Set show_in_rest if you want the block editor.
  • Flush permalinks once after registering.
  • Add templates only when the default layout is not good enough.