
How to Create Custom Post Types Using Function.php
On your WordPress website, post types are used to help distinguish between different content types in WordPress. Posts and pages are both post types but are made to serve different purposes.
You can also create your own post types, known as custom post types. These are useful when creating content that has a different format than a standard post or page.
At the time of adding a post type, you always need to register taxonomies of that post type. You can define and register a taxonomy using register_taxonomy() function. The register_post_type() function is used to create a post type and it only be called through the init action hook.
WordPress custom post types allow you to create and manage different types of content in addition to the default posts and pages. Custom post types can be used to create anything from products, events, portfolio items, testimonials, to any other kind of content that requires a unique structure.
For example, let’s say you have a website for a restaurant and you want to add a custom post type for your menu items. You can create a custom post type called “Menu Items” with custom fields such as “Item Name”, “Description”, “Price”, “Ingredients”, etc. You can also create custom taxonomies such as “Cuisine Type” or “Dietary Restrictions” to categorize your menu items.
Another example could be a real estate website where you can create a custom post type called “Properties” with custom fields such as “Address”, “Property Type”, “Price”, “Number of Bedrooms”, “Number of Bathrooms”, etc. You can also create custom taxonomies such as “Neighborhood” or “Property Features” to help your visitors find the properties they are looking for.
Custom post types are very flexible and can be used to create any type of content that requires a specific structure and organization. They allow you to manage and display your content in a more efficient and customized way, making it easier for your visitors to navigate and find what they are looking for.
You only need to open and modify functions.php file which is located in /wp-content/themes/theme_name/functions.php. So open the functions.php file and copy and paste following code:
// Job Listing Custom Post Type function job_init() { // set up Job Listing labels $labels = array( 'name' => 'Job Listings', 'singular_name' => 'Job Listing', 'add_new' => 'Add New Job Listing', 'add_new_item' => 'Add New Job Listing', 'edit_item' => 'Edit Job Listing', 'new_item' => 'New Job Listing', 'all_items' => 'All Job Listing', 'view_item' => 'View Job Listing', 'search_items' => 'Search Job Listings', 'not_found' => 'No Job Listings Found', 'not_found_in_trash' => 'No Job Listings found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Job Listings', );
// register post type $args = array( 'labels' => $labels, 'public' => true, 'has_archive' => true, 'show_ui' => true, 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array('slug' => 'job_listing'), 'query_var' => true, 'menu_icon' => 'dashicons-randomize', 'supports' => array( 'title', 'editor', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'thumbnail', 'author', 'page-attributes' ) ); register_post_type( 'job_listing', $args ); // register taxonomy register_taxonomy('job_category', 'job_listing', array('hierarchical' => true, 'label' => 'Category', 'query_var' => true, 'rewrite' => array( 'slug' => 'job-category' ))); } add_action( 'init', 'job_init' );