What is Custom Post Types in WordPress And How To Register It
A Custom Post Type is a unique feature or WordPress CMS. You can use it to manage and organize different types of data. For Example: Testimonials, Portfolios, Events etc.
By default, WordPress only have two post types which are Posts and Pages. If you need to add more custom post types, you need to add a piece of code into your theme’s function.php file. You can also use a Custom Post Type UI Plugin which is also commonly known method among developers to create CPTs without any use of code.
Custom Post Types help you to keep your content organize and distinct from each other making it easier to manage.
Example Usage:
You are creating a website related to Property Listings. You will need a custom post type related to Properties in your website to manage all the properties separated from your blog post. In each property, you will need some custom fields such as price, location, size etc.
Code To Paste In Your functions.php File
Go To wp-content/themes/activethemefolder/functions.php and paste anywhere before ending php?> or ?>
function create_properties_post_type() {
$labels = array(
'name' => _x('Properties', 'post type general name'),
'singular_name' => _x('Property', 'post type singular name'),
'menu_name' => _x('Properties', 'admin menu'),
'name_admin_bar' => _x('Property', 'add new on admin bar'),
'add_new' => _x('Add New', 'property'),
'add_new_item' => __('Add New Property'),
'new_item' => __('New Property'),
'edit_item' => __('Edit Property'),
'view_item' => __('View Property'),
'all_items' => __('All Properties'),
'search_items' => __('Search Properties'),
'not_found' => __('No properties found.'),
'not_found_in_trash' => __('No properties found in Trash.')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'properties'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'),
);
register_post_type('properties', $args);
}
add_action('init', 'create_properties_post_type');