Skip to main content

Create custom posts in WordPress

By Sue Johnson

As well as pages and posts, WordPress has the ability to use custom posts. These are useful for special pages which don’t fit within pages or blog posts. For example, custom posts can be used to create a portfolio or something like pages of books or recipes. WordPress was originally a blogging system, the introduction of custom posts means that it can be used as a content management system.

It is possible to create custom posts within the theme, however this is bad practice because if you change the theme all your custom posts will be lost.

It is better to create a plugin. Here is how you can create a plugin for your custom posts.

Create a basic plugin

Create a folder in the wp-content plugins directory. Call it whatever you want your plugin to be called, in this case ‘suesdesign-custom-posts’. Pick a unique name so it does not clash with other plugins. Create a PHP file with the same name, in this example, ‘suesdesign-custom-posts.php’.

At the top of suesdesign-custom-posts.php you will need to put the plugin name in the comments. You can also include other information about the plugin.

<?php
/*
Plugin Name: Suesdesign Custom Posts
Plugin URI: http://suesdesign.co.uk/
Description: Example of custom posts
Author: Sue Johnson
Version: 1.0
Author URI: http://suesdesign.co.uk/
*/

?>

Register custom post type

Create a function which registers the custom post type and gives it a set of labels.

You need to give your post type a name between 1 and 20 characters.

The _x() and __() which appear around each label are for translating the plugin into other languages, and it is good practice to include these.

You can add whatever you want the custom posts to support, in this example thumbnails (featured images), title and default editor is supported.

function suesdesign_custom_register_post_type() {
    $labels = array(
        'name'               => _x( 'Custom Posts', 'suesdesign_posts' ),
        'singular name'      => _x( 'Custom Post', 'suesdesign_posts' ),
        'add_new'            => _x( 'Add new Custom Post', 'suesdesign_posts' ),
        'add_new_item'       => __( 'Add new Custom Post', 'suesdesign_posts' ),
        'edit_item'          => __( 'Edit Custom Post', 'suesdesign_posts' ),
        'new_item'           => __( 'New Custom Post', 'suesdesign_posts' ),
        'all_items'          => __( 'All Custom Posts', 'suesdesign_posts' ),
        'view_item'          => __( 'View Custom Post', 'suesdesign_posts' ),
        'search_items'       => __( 'Search Custom Posts', 'suesdesign_posts' ),
        'not_found'          => __( 'No Custom Posts', 'suesdesign_posts' ),
        'not_found_in_trash' => __( 'No Custom Posts found in trash', 'suesdesign_posts' )
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'has archive' => true,
        'supports' => array( 'title', 'editor', 'thumbnail' )
    );
   
    register_post_type( 'suesdesign_posts', $args );
}

You then need to add this as an action which adds the custom posts when the website page is loaded.

add_action( 'init', 'suesdesign_custom_register_post_type' );

Dealing with permalinks

At this stage if you activate the plugin, create your custom posts and try to load the pages it will come up with a 404 page not found error.

You can switch the permalinks off and back on again or you can have this happen automatically by flushing the permalinks when the plugin is activated. This is better for plugins which will be distributed and used on other websites.

register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
register_activation_hook( __FILE__, 'suesdesign_custom_posts_flush_rewrites' );
function suesdesign_custom_posts_flush_rewrites() {
// call custom posts registration function
suesdesign_custom_register_post_type();
flush_rewrite_rules();
}

Once the plugin is activated, you will see the custom posts now appear in your WordPress menu.

Custom posts

Create page templates for the custom posts

Finally you will need to create custom templates for the custom posts. You will need a page to display a list of posts. You might also want to create pages to display each individual post. How to do this is here: https://suesdesign.co.uk/create-templates-for-custom-posts-in-wordpress/

The code for the complete plugin for both tutorials is on GitHub here: https://github.com/suesdesign/suesdesign-custom-posts

What will you do with custom posts?

All comments will be held in a moderation queue. A selection will be published.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.