https://rapidweb.biz/wp-content/uploads/2017/05/custompost-1024x512-1024x512.png

Creating Custom Post Types in WordPress


Custom post types are the same as a basic post type that are already present in WordPress. However, the beauty of custom post types is that you can tweak them to your individual needs, allowing for great customisation. Custom post types are perfect for those projects where the default post type just isn’t up to the task.

In this blog post, I will be going over how to create a simple custom post type in WordPress and then provide a number of extras which you can add on top which you may find useful.

Adding a Custom Post type to your project

Basic Example

This first example is a very basic custom post type named ‘Album’.
To add this to your project open up your WordPress project and locate the functions.php file, which should be located inside themes/yourthemename/.

Copy the snippet of code below into the functions.php file.

function create_album_post_type() {
    $args = array(
      'public' => true,
      'label'  => 'Album'
    );
    register_post_type( 'album', $args );
}
add_action( 'init', 'create_album_post_type' );

In the example above the only parameters we are passing through are the name of the post type Album and the public status which we are setting as true, which will make the post type visible to authors.

Now although the example above will work, it is a bit lacking in ‘usefulness’. Fortunately for us there are plenty of additional parameters we can provide that will let us spice up our custom post type making it much more useful.

Let’s do a quick overview of some of the additional parameters which I have found the most useful when developing.

Supports

'supports' => array('title', 'editor', 'thumbnail', 'excerpt')

By default when a user goes to create a custom post type they will only be able to see the title and editor fields. With the code above you can pass through other WordPress edit options such as thumbnail and excerpt. See the official WordPress documentation for additional options.

However, if you’re using the popular Advanced Custom Fields plugin, you can simply create your own field groups for the custom post type in place of the default WordPress options.

Has Archive

'has_archive' => true

This allows for a ‘listing’ page of your custom post type. By default WordPress will use the archive.php file in your selected theme. However, if you wanted a separate archive page for your own custom post type you can create a file in the same directory named archive-REGISTER-POST-NAME.php. Using my album example above, as I registered the custom post type via register_post_type( 'album', $args );, I would have to name the custom archive file as archive-album.php.

Once you have created the new file simply copy the content of archive.php into your new file, and then tweak it to your hearts content.

Taxonomies / Categories

'taxonomies' => array( 'category' )

This snippet allows you to add taxonomies to your custom post type. The simplest one to add is the Categories, a taxonomy that is already present in WordPress and already available on the default post type. You can also create your own custom taxonomies and add them instead or choose them both!

Renaming default Posts to News Articles in the Admin Area

function change_post_menu_label() {
    global $menu;
    global $submenu;
    $menu[5][0] = 'News Articles';
    $submenu['edit.php'][5][0] = 'News Articles';
    $submenu['edit.php'][10][0] = 'Add News Article';
    echo '';
}
function change_post_object_label() {
        global $wp_post_types;
        $labels = &$wp_post_types['post']->labels;
        $labels->name = 'News Articles';
        $labels->singular_name = 'News Article';
        $labels->add_new = 'Add News Article';
        $labels->add_new_item = 'Add News Article';
        $labels->edit_item = 'Edit News Article';
        $labels->new_item = 'News Article';
        $labels->view_item = 'View News Article';
        $labels->search_items = 'Search News Articles';
        $labels->not_found = 'No News Articles found';
        $labels->not_found_in_trash = 'No News Articles found in Trash';
}

add_action( 'init', 'change_post_object_label' );
add_action( 'admin_menu', 'change_post_menu_label' );

Although this snippet is not for custom post types I still think it’s a useful bit of code to include. This snippet changes the display name of ‘Posts’ to ‘News Articles’. We’ve starting doing this on a number of projects to make things clearer for the clients, as to the ordinary person not familiar with WordPress ‘Posts’ means nothing to them. So changing the display name to ‘News Articles’ is more informative and intuitive for the clients.

Likewise with the above, to add this to your project insert the code into the functions.php file.

Set an icon

'menu_icon'   => 'dashicons-products',

To customise your post type even further, WordPress allows you alter the icon that appears in the admin menu. The above snippet uses an icon from the dashicons font, which is the offical icon font of the WordPress admin. To see all the icon options visit the official WordPress documentation.

However if you want to set an icon to be your own image for example, you can simply replace the ‘dashicons-products’ above to be the url of your chosen image. Which you can see below.

'menu_icon'   => 'http://www.yoursite.com/wp-content/uploads/2017/05/your-new-fancy-icon-image.png',

For more information on custom post types head over to the official WordPress documentation here.