How to Create WordPress Custom Post Types

How to Create Custom Post Types in WordPress

A custom post type is just a generic post with a database post type attribute that is distinct.

Home / Blog / Wordpress / How to Create Custom Post Types in WordPress

For a few years, WordPress has been gaining popularity as a general CMS, but the custom post type mechanism, which allows the development of a large range of content, was the true game-changer. Let’s take a look at how this came to be and all of the possibilities that this fantastic feature has.

There are several different types of content in WordPress, and they are categorized into post types. A post is a single object, but it’s also the name of a regular post type called a post. WordPress comes with a variety of different post types by default, all of which are contained in the database under the wp posts table.

Now, let’s look at what WordPress can do for you

A custom post type is just a generic post with a database post type attribute that is distinct. Regular posts are of the “post” post type, while pages are of the “page” post type, and attachments are of the “attachment” post type, and so on. You can now make your own to show the type of content you’ve made. For novels, movies, ratings, and items, for example, you can make custom post types.

With only a few lines of code, you will obtain the following results:

  • The custom post type will appear as a separate menu item in the backend, with its own post list and “add new” page.
  • The archive page for the post type can be found by going to http://example.com/customposttype/. This is equivalent to searching on the front page for the most recent posts with the “post” post type.
  • You may make categories and tags accessible to the custom post type, or build custom taxonomies.

Default Custom Post Types

Unless removed, the following post types are always included in a WordPress installation:

  • Posts
  • Pages
  • Attachments
  • Revisions
  • Navigation Menus
  • Custom CSS
  • Changesets

Creating Custom Post Types

The first thing on our to-do list is to build the post type itself. When dealing with custom post types, it’s better to build a plugin, but if you don’t know how or only need a short test, you can use your theme’s functions.php file.

It will build a post type with absolutely no customization at its most basic shape. It won’t be visible to the public, won’t be in the admin, and contact notifications will be the same as for posts (“post saved,” “post updated,” etc.). We’ll  go over some of the most commonly-used alternatives and add them to the previously empty $args list to tailor our current post type to our needs.

<?php

function pe_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ), 
    'parent_item_colon'  => __( 'Parent product' ),
    'menu_name'          => 'Products'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'product', $args ); 
}

add_action( 'init', 'pe_custom_post_product' );
  • labels This can be an array that defines the various labels a custom post type may have. I broke this out earlier to make the case for registering a post type more transparent.
  • description A quick overview of our custom post type, including what it does and why we use it.
  • public This choice manages a number of items at once. If you set this to true, plenty of other possibilities would also be true. It is possible, for example, to make a custom post type available but not queryable.

Once you’ve done that, the custom post type should appear in the menu. You should be able to create new posts, browse the post list in the admin, and visit the website’s existing posts.