How To Register Custom Post Type Using Code

Are you looking to create and register a custom post type on your WordPress website? In this article we will share how you can register a custom post type using code.

What is a custom post type?

Custom post type is similar to a post or page but with a custom structure. This custom post type can have custom fields as per the post structure you need. For example, you can have a custom post type for products. Where you can have custom fields for the product name, description and other custom fields. You can also have custom category structure.

Why using code is better then a plugin

While you can use a plugin to make your job easier, there are many disadvantages of using a plugin. One of them is, if you uninstall the plugin the custom post type will get deactivated with it. The custom post type will also be unregistered and you will not be able to access it from your admin dashboard.

Registering a custom post type using Code in WordPress

A custom post type can be registered by adding the below code in the theme’s functions.php file.

function custom_post_type_products() {

// Set label names for custom post type

$labels = array(

'name' => _x( 'Custom Products', ‘General name' ),

'singular_name' => _x( 'Custom Product, 'Singular name' ),

);

// Various options for the custom post type

$args = array(

'label' => __( 'products’, 'theme-name ),

'labels' => $labels,

'description' => 'A custom post type',

'public' => true,

'hierarchical' => false,

'show_ui' => true,

'show_in_menu' => true,

'show_in_nav_menus' => true,

'show_in_admin_bar' => true,

'menu_position' => 5,

'can_export'  => true,

'has_archive'  => true,

'exclude_from_search' => false,

'publicly_queryable' => true,

'capability_type' => 'post',

'show_in_rest' => true,

);

// The below line registers the custom post type with the above arguments.

register_post_type( ‘products', $args );

}

// The custom post type function is now hooked to the init function

add_action( 'init', custom_post_type_products, 0 );

In the above code you will have to replace products with your custom post type name.

$labels

$labels is an array that has data regarding the menu names and the name of the custom post type.

name

This is the plural name for the custom post type

singular_name

The name for a single custom post

$labels can also have other elements such as add_new, add_new_item, edit_item featured_image, set_featured_image and menu_name.

$args

$args is the variable name used for an array  that is going to store the various elements of the custom post type. There are many elements in args, below are some of the most commonly used.

description

This is used to describe the custom post type in a short summary.

public

This field sets the visibility of the custom post type for the visitors and authors. It should be set to TRUE to make the custom post type visible to everyone.

show_in_nav_menus

Setting it to TRUE makes it possible to add the custom post type to the navigation menus.

menu_position

Using the menu position you can set the position of the custom post type in the admin menu section. You will have to refer to the WordPress Codex for values for various positions. Using a menu position of 5 aligns it below the Posts.

Displaying the custom post type on the Front Page

By default custom post types are hidden from the front page. To display them you have to add the below code to your functions.php file.

 

add_action( 'pre_get_posts', add_custom_post_type );



function add_custom_post_type( $query ) {

if ( is_home() && $query->is_main_query() )

$query->set( 'post_type', array( 'post', customposttypename' ) );

return $query;

}

 

Have questions or confused about something WordPress Related? Join Our Discord Server & ask a Question

Leave a Comment

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

Scroll to Top