Dani 1 min read
January 17, 2024 (last revised)

How to Create Custom Post Types in WordPress with PHP

A custom post type (CPT) in WordPress offers a potent method to organize and showcase distinct content on your website. Unlike traditional posts and pages, a CPT enables you to tailor the structure to your precise requirements, providing a more refined user experience. For example, you can use it to spotlight a portfolio item, testimonial, product, or event.

<?php

// Register Custom Post Type
function custom_post_type() {
    $labels = array(
        'name'               => 'Custom Posts',
        'singular_name'      => 'Custom Post',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Custom Post',
        'edit_item'          => 'Edit Custom Post',
        'new_item'           => 'New Custom Post',
        'all_items'          => 'All Custom Posts',
        'view_item'          => 'View Custom Post',
        'search_items'       => 'Search Custom Posts',
        'not_found'          => 'No custom posts found',
        'not_found_in_trash' => 'No custom posts found in Trash',
        'menu_name'          => 'Custom Posts',
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'custom-posts' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => 5,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
    );

    register_post_type( 'custom_post', $args );
}

add_action( 'init', 'custom_post_type' );

This code registers a CPT named ‘custom_post’ with various labels and arguments, defining how it appears and functions within the WordPress admin and front-end. The provided arguments include settings for visibility, UI display, rewrite rules, capabilities, archives, and supported features. The add_action hook ensures the custom post type is registered during the initialization process in WordPress.

Custom post type

Custom Post Type code explanation:

Here’s an explanation of the code provided:

  1. Register CPT:
    • The custom_post_type function registers a new CPT called “Custom Posts” with various labels and settings.
  2. Labels and Args:
    • Labels define how the CPT appears in the WordPress admin.
    • Args (arguments) specify various settings, such as public visibility, menu placement, and supported features.
  3. Register Post Type:
    • The register_post_type function is called with the custom post type name (‘custom_post’) and the provided arguments.

By adding this code to your theme’s functions.php file, you’ll create a new CPT accessible from the WordPress admin menu.

One major advantage is improved content management, as each post type can have its own set of custom fields and taxonomies. Additionally, the ability to create archives for custom post types means you can present information in a more structured and user-friendly manner.

Creating custom post types is a pivotal aspect of web development. If you’re curious about the nuances between Bootstrap and Tailwind, check out our guide on Comparing Bootstrap and Tailwind for Web Development to make informed decisions in your design journey.

Admin Menu PHP Wordpress