WordPress Custom Post Type

Introduction

WordPress custom post type is any type that is not one of the 5 default post types:

  • Post (Post Type: ‘post’)
  • Page (Post Type: ‘page’)
  • Attachment (Post Type: ‘attachment’)
  • Revision (Post Type: ‘revision’)
  • Navigation menu (Post Type: ‘nav_menu_item’)

In order to create a new wordpress type, we need to create a custom function that will activate an option in the admin panel with our specific requirements. The entire custom function is essentially based on the register_post_type() function.

Function register_post_type()

$post_type
is the name used by wordpress so called machine name, must not contain capital letters or spaces

$args is an array of arguments that define the new post type.
Some frequently used arguments are:

  • label is the name (plural) that appears on the admin page ready for translation
  • labels is an array of labels used in that type of custom post
    • name’ – general name for the post type, usually plural. The same as, and overridden by $post_type_object->label
    • singular_name’ – name for one object of this post type. Defaults to value of ‘name’.
    • menu_name’ – the menu name text. This string is the name to give menu items. Defaults to a value of ‘name’.
    • name_admin_bar’ – name given for the “Add New” dropdown on the admin bar. Defaults to ‘singular_name’ if it exists, ‘name’ otherwise.
    • all_items’ – the all items text used in the menu. Default is the value of ‘name’.
    • add_new’ – the add new text. The default is “Add New” for both hierarchical and non-hierarchical post types. When internationalizing this string, please use a gettext context matching your post type. Example: _x('Add New', 'product');
    • add_new_item’ – the add new item text. Default is Add New Post/Add New Page
    • edit_item’ – the edit item text. In the UI, this label is used as the main header on the post’s editing panel. The default is “Edit Post” for non-hierarchical and “Edit Page” for hierarchical post types.
    • new_item’ – the new item text. Default is “New Post” for non-hierarchical and “New Page” for hierarchical post types.
    • view_item’ – the view item text. Default is View Post/View Page
    • search_items’ – the search items text. Default is Search Posts/Search Pages
    • not_found’ – the not found text. Default is No posts found/No pages found
    • not_found_in_trash’ – the not found in trash text. Default is No posts found in Trash/No pages found in Trash.
    • parent_item_colon’ – the parent text. This string is used only in hierarchical post types. Default is “Parent Page”.
  • description description
  • public is an option that controls visibility to site authors and readers, and directly affects other parameters exclude_from_search, publicly_queryable, show_in_nav_menus, and show_ui because it gives their default value.
    It is a boolean value so it can be:
    • true’ The built-in types attachment, page, and post are similar to this.
    • false’ The built-in types nav_menu_item and revision are similar to this. Best used if you’ll provide your own editing and viewing interfaces (or none at all).
  • menu_position
    • 5 – below Posts
    • 10 – below Media
    • 15 – below Links
    • 20 – below Pages
    • 25 – below comments
    • 60 – below first separator
    • 65 – below Plugins
    • 70 – below Users
    • 75 – below Tools
    • 80 – below Settings
    • 100 – below second separator
  • menu_icon can be:
    • url to icon e.g. ‘get_template_directory_uri().”/images/cutom-posttype-icon.png”‘
    • icon name from iconfont e.g. ‘dashicons-video-alt’
  • supports is a boolean string
    • ‘title’
    • ‘editor’ (content)
    • ‘author’
    • ‘thumbnail’ (featured image, current theme must also support post-thumbnails)
    • ‘excerpt’
    • ‘trackbacks’
    • ‘custom-fields’
    • ‘comments’ (also will see comment count balloon on edit screen)
    • ‘revisions’ (will store revisions)
    • ‘page-attributes’ (menu order, hierarchical must be true to show Parent option)
    • ‘post-formats’ add post formats

      Make sure theme level feature support is provided before doing custom post type level support!

  • has_archive is a boolean value that activates post type archives, so $post_type will be used as the archive slug by default.

Custom post type through examples

Basic ease of use:

or a complete example:

NOTE:
If a 404 error appears after registering a post type, it is necessary to Save Changes in Settings/Permalinks.

Text adaptation in the admin interface

Changing the default messages that wordpress triggers after some user actions can be easily changed using post_updated_message hooks as in the following example.

Or similar:

Help text for explanations

image

image ×

To facilitate the use of custom post types, a help tab can be created that would appear in the admin page in two “places”

  • when we review the list with all custom post types
  • when we edit a custom post type

This kind of help menu is obtained by embedding a custom function in functions.php, which is activated with the action hook contextual_help. The following example shows the code for the help menu on the admin page for custom post type movie:

image

image ×

In the previous example, the help tab with its help text will appear on two different occasions and will contain different accompanying texts. While we are in the Admin page and when:

  1. we review all posts of this type
    (the part in the code when ‘movie’ == $screen->id was used)
  2. edit individual post
    (the part in the code when ‘edit-‘movie” == $screen->id was used)

If we want to have more tabs and we want it to be saved for translation, we will use the code as in the following example:

Custom Post Type Templates

Showing one member

The template single.php is used to display a single member of a special type, but if there is a need to display it differently than a regular single post, we must create a special template with the name single-TypeName.php.

List display

By default, the standard template archive.php is used to display the list of special types, and if we want it to be more specific, then we use the template called archive-NameTipa.php

NOTE:
In order to create a menu-link in the navigation that opens a list with all posts of the custom type type, we will use the custom link option, where we will put http://domen.com/NazivCustomPostType as the URL.

Explanation with examples

If, for example, custom post type “films”, it follows that when we want to display a single film (eg “Terminator”) WP will use the single-filmovi.php template, but if we want to display all films entered through the custom post type, the archive-filmovi.php template is used.

Displaying Custom post type

Displaying together with standard posts

One of the advantages of custom post types is that they are separated from “ordinary” posts ie. they are displayed on the blog page along with “regular” posts. But if we still want to combine them, we have to use the following code:

The previous code uses the action hook pre_get_posts which is called after the creation of the $query object but before it is started in the loop. Therefore, at the moment our hook works, the code will be activated that adds a new custom post type “movie” to the $query object along with the standard posts.

Appearance in some template

If we want to display a list of our custom post type inwithin a template, then custom loop:

is used

Displaying in the widget area

In order to display a list of recent custom post types, we will use the “Ultimate Post Widget” plugin, which after activation and placing it as a widget in the sidebar allows you to see recent posts of any type.