Follow Us :
Create Custom Post in WordPress

How can you create Custom Post Type in WordPress?

Facebook
Twitter
LinkedIn
WhatsApp

WordPress is often misunderstood as a blogging platform, but it has evolved into a robust content management system (CMS) over the years. One of its key features is the ability to create custom content types, known as Custom Post Types (CPTs). These can be tailored to suit specific needs and are not limited to blogging.

What is a Custom Post Type?

WordPress has five built-in post types: Post, Page, Attachment, Revision, and Navigation Menu. Custom Post Types allow you to create additional types of content beyond these standard options. For instance, when designing a WordPress website in Mississauga, you might want to create a “Restaurant Review” post type with custom fields and categories to cater to their specific needs.

Creating a Custom Post Type Manually

To create a custom post type manually, you can add the necessary code to your theme’s functions.php file. This approach ensures that the custom post type remains even if you deactivate the plugin used to create it.

Example Code

php

// Our custom post type function function
create_custom_post_restaurant() {

register_post_type( 'restaurant',

// Custom Post Type Options

array(

'labels' => array(

'name' => __( 'Restaurant' ),

'singular_name' => __( 'Restaurant' )

),

'public' => true,

'has_archive' => true,

'rewrite' => array('slug' => 'restaurant'),

)

);

}

// Hooking up our function to theme setup

add_action( 'init', 'create_custom_post_restaurant' );

Explanation:

  • Function Definition: The code defines a function named create_custom_post_restaurant that registers a custom post type.

  • Registering the Custom Post Type: The register_post_type function is used to register the custom post type. The first argument is the name of the post type, which in this case is 'restaurant'.

  • Custom Post Type Options: The second argument is an array of options for the custom post type. This array includes:

    • Labels: An array of labels for the post type. The name label is used as the plural name for the post type, and the singular_name label is used as the singular name.
    • Public: A boolean indicating whether the post type should be publicly accessible.
    • Has Archive: A boolean indicating whether the post type should have an archive page.
    • Rewrite: An array of rewrite rules for the post type. In this case, the slug for the post type is set to 'restaurant'.

  • Hooking the Function: The add_action function is used to hook the create_custom_post_restaurant function to the init action. This ensures that the function is executed when the WordPress initialization process begins.

Output:

When you run this code, it will register a custom post type named “Restaurant” with the following properties:

  • Name: “Restaurant”
  • Singular Name: “Restaurant”
  • Public: True
  • Has Archive: True
  • Rewrite Slug: “restaurant”

    This custom post type can then be used to create and manage custom content on your WordPress site.

Get Our Latest Tips & Tricks Updated

error: Content is protected !!