What are custom post types?

Wordpress 22 Dec 2021 0
What are custom post types?

Custom post types (CPT) represent one of the most powerful tools in your WordPress site. If you want to start a new blog or website you should acquire even a basic knowledge of them.

But take a step back, what are custom post types? And most of all how can I use them? Let’s start the journey.

What are custom post types

This is the official CPT definition taken from the official WordPress website:

WordPress houses lots of different types of content and they are divided into something called Post Types. A single item is called a post however this is also the name of a standard post type called posts. By default WordPress comes with a few different post types which are all stored in the database under the wp_posts table.

Ok, it sounds a little bit weird, what does it mean?

Basically WordPress content lays in the same storage space (in technical terms the same MySQL table).

As you probably know, in the origin WordPress was a simple blogging platform and there was just one single type of content, the posts. Today WP is a robust CMS, but the term post stuck to it. However, a post type can be any kind of content.

By default, WordPress comes with these post types:

  • Post
  • Page
  • Attachment
  • Revision
  • Nav Menu

As you may imagine Attachment, Revision and Nav Menu are used internally by WordPress to hande uploads, revisions and navigation menus.

Why custom post types are so important?

Let’s take a popular example.

You probably know WooCommerce plugin, which converts your WordPress website into a fully functional ecommerce.

How this is possible? well, CPT is the answer.

Under the hood, WooCommerce creates a new custom post type called product and uses it for building its entire code logic.

When do I need a custom post type?

You need a CPT every time you want to create some particular content different from native post/page.

Can I create a new custom post type?

Yes! you can create your own custom post types and call them whatever you want.

WordPress offers you a robust set of APIs and functions to register and manage new custom post types. You can choose between creating CPT manually or using a plugin.

Ok let’s pretend to run a movie review website; the first step is to create a new custom post type called movie. How can we do this?

Creating a custom post type manually

In order to create a new custom post type you should use WordPress register_post_type native function.

Add this code to your functions.php file:

// Our custom post type function
function create_posttype() {
 
    register_post_type( 'movies',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Movies' ),
                'singular_name' => __( 'Movie' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'movies'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

Please note that this is the minumum implemention of register_post_type function.

What this code does is that it registers a new post type called movies with an array of arguments. These arguments are the options of our custom post type.

This array has two parts, the first part is labeled, which itself is an array. The second part contains other arguments like public visibility, has archive, slug, and show_in_rest enables block editor support.

We’ll review this function more deeply the near future.

Creating a custom post type with ACPT

The easiest way to create a custom post type in WordPress is by using a plugin. This method is recommended for beginners because it is safe and super easy.

There are a lot of good plugins available on the market. Here you can find the 8 Best Custom Post Types Plugins for WordPress 2021.

ACPT is a small but very ambitious brand new plugin.

You can start downloading ACPT Lite.

After installing the plugin you will find a new label on admin menu. Click on it.

Wow, a fresh ACPT install!

Ok, let’s create what we need. Click on Register new Post Type button.

Step 1 – we have to fill these fields:

  • Post name – type movie
  • Singular label – type Movie
  • Plural label – type Movies
  • Icon – choose the icon you want

For now, keep the other stuff as is, click on the next step.

Step2 – Click on the next step.

Step3 – Click on Save.

You’re done! Congratulations you have created your first custom post type with ACPT! And it took seconds!

The journey has started!

Comments

There are no comments yet

Please Post Your Comments & Reviews

20% Discount!


Use this code during check-out:

ACPT_2024_PROMO_20

Valid for all products*

*This coupon code is valid for all products and plans. The promotion expires October, 31st 2024 00:00:00 UTC

This will close in 20 seconds