Custom Post Types in WordPress: Use and plugins

One of the things that I value the most about WordPress as CMS (or content manager) is its scalability, that is, its ability to adapt to the growth of our website without having to change to other more specific managers. One of the options that allow us to extend the base functionalities of the Core and adapt WordPress to our project is without a doubt the Custom Post Types (CPT.)

A Custom Post Type is a new type of input that we add to our WordPress with the aim of managing its contents independently of the pages and the normal posts of the blog. In this way we can have different sections on our website with contents that are updated independently.

What is a Custom Post Type in WordPress?

Imagine a website about book reviews as an example to better understand the use of Custom Post Type:

  • We would have a section devoted to the blog, where we would write about news, new releases of books, presentations, interviews with the authors … all this would go in the entries section.
  •  There would be a series of static pages with information about the site, a contact page, privacy policy, services, advertising … in short, content that will not change and that we will place in the navigation menu. All these contents would go in Pages.
  • Criticisms and reviews of books with ratings and recommendations about them. We also need to be able to filter them by topic or authors, and of course a search engine. All this will go in a new Customized Entry Type that we will call Books.
custom-post-type-wordpress
This shows the types of custom entries on the WordPress desktop

WordPress provides by default the first two types of content (entries and pages,) but if we want to expand the catalog, we will have to use plugins or custom code to achieve it. Other examples that can serve to illustrate the use of CPT are the products in an online store (in fact WooCommerce creates a Custom Post Type when installed to manage the products,) movies, portfolio, testimonials, services … you can create so many types of custom posts as you need.

How to create a Custom Post Type

Let’s see the options we have when creating a Custom Post Type in WordPress. Before going on, it is important to be clear on one issue: since it is a functionality that we are going to add, it must always be in the form of a plugin, in order to be able to freely change the subject without losing the contents of the new type of custom post. CPTs are plugin territory and this territory should not be invaded from the WordPress theme (either through the parent theme or the child theme.)

Plugins to create Custom Post Types

We have compiled several options that make life easier in the creation and edition of these types of personalized entries through plugins.

pods-custom-post-type

Pods is a very complete plugin that allows (among many other options) to create a CPT in a couple of clicks. We can also extend the options of the other types of entries and add taxonomies (such as categories and labels) to the new types of entries.

wck-custom-post-type

WCK, a plugin also very complete, with the option to create, edit and modify the types of entries, besides being able to add custom fields (continuing with the web about book criticism, the custom fields would be Author, year of edition, editorial …)

custom-post-type-plugin-generator

Custom Post Type Generator, lighter and easier than the previous ones, it allows us to create the Custom Post Type in a couple of clicks and without too many complications. If you already know what you need and you are clear that you are not going to extend functionalities or need taxonomies, this is your plugin.

Types of custom posts in Jetpack

Although it is very limited, Jetpack offers the option to activate two types of new entries, portfolio and testimonials. We will only have to navigate in the settings to write options, and activate the type of input we need. For now we only have these two options available, but if your project requires them and you are already using the Jetpack plugin for other functions, what is the need to install a new plugin?

jetpack-custom-post-type

In addition to these two types of entries, making some adjustments to our theme (if it is in our child theme much better) we have the option to activate a couple of types of entries: Food menu and Comics, for this you must add support in your theme according to the documentation.

Custom code to create CPT

If you are not afraid to touch some PHP and learn new ways about WordPress, then let’s review how to create a Custom Post Type from scratch. In the following example we will register a new type of personalized post to organize our books, with the following options:

add_action( 'init', 'register_book' );
/**
 * Register a book post type.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
function register_book() {
	$labels = array(
		'name'               => _x( 'Books', 'post type general name', 'silocreativo' ),
		'singular_name'      => _x( 'Book', 'post type singular name', 'silocreativo' ),
		'menu_name'          => _x( 'Books', 'admin menu', 'silocreativo' ),
		'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'silocreativo' ),
		'add_new'            => _x( 'Add New', 'book', 'silocreativo' ),
		'add_new_item'       => __( 'Add New Book', 'silocreativo' ),
		'new_item'           => __( 'New Book', 'silocreativo' ),
		'edit_item'          => __( 'Edit Book', 'silocreativo' ),
		'view_item'          => __( 'View Book', 'silocreativo' ),
		'all_items'          => __( 'All Books', 'silocreativo' ),
		'search_items'       => __( 'Search Books', 'silocreativo' ),
		'parent_item_colon'  => __( 'Parent Books:', 'silocreativo' ),
		'not_found'          => __( 'No books found.', 'silocreativo' ),
		'not_found_in_trash' => __( 'No books found in Trash.', 'silocreativo' )
	);

	$args = array(
		'labels'             => $labels,
        'description'        => __( 'Description.', 'silocreativo' ),
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'slug' => 'book' ),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		'menu_position'      => 5,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
	);

	register_post_type( 'book', $args );
}

Some interesting comments:

Position order of the Custom Post Type on the WordPress desktop

  • 'rewrite' => array( 'slug' => 'book' ): allows us to customize the url where the books will appear. Therefore, in this example, all titles that we add will be under the / book / in the url. For example: misitioweb.com/book/los-pilares-de-la-tierra
  • 'menu_position' => 5: manages the position of the new element added within the sidebar of the WordPress admin area
  • 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ): This is where you define what elements will be available in this new type of entry: title, editor, author selector, prominent image …

The other options define the available variables; you can read more in the codex. It is important to replace “silocreativo” with the text-domain that you use for your theme (or plugin) for the translation. If it is going to be in a single language, you can edit the texts directly without going through the translation files.

Custom post types generator

If you are going to add your own code through a plugin, but you want to make your life easier, you can also choose to generate the necessary code through GenerateWP. With this tool we can introduce the information as a questionnaire and the internal part so it will generate the necessary code; it is perfect if it is the first time you try to create a Custom Post Type or if you want to review concepts.

Although the operation is good, it is always good to review the resulting code in case some parameters are not well established.

And you, did you already know the CPT? What do you use it for? I hope the article was helpful.

Leave a Reply

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