How to make custom posts and assign custom categories(taxonomies) to it in wordpress?

wordpress

We can use custom posts and categories, so that we can customize wordpress as we like . If you are attempting to build a real estate website using wordpress, it would be great if you have something like properties and property categories rather than post and categories right? Thats what we are attempting to achieve here.

wp1

wp2

Complete Code


add_action( 'init', 'eventcustom_init' );
function eventcustom_init() {
$argsPost = array( 'public' => true, 'label' => 'Events','taxonomies'=>array('eventtype') );
register_post_type( 'events', $argsPost );
$labels = array(
'name' => _x( 'Event Type', 'taxonomy general name' ),
'singular_name' => _x( 'Event Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Events Type' ),
'all_items' => __( 'All Event Type' ),
'parent_item' => __( 'Parent Events Type' ),
'parent_item_colon' => __( 'Parent Event Types:' ),
'edit_item' => __( 'Edit Event Type' ),
'update_item' => __( 'Update Event Type' ),
'add_new_item' => __( 'Add New Event Type' ),
'new_item_name' => __( 'New Event Type Name' ),
'menu_name' => __( 'Event Types' ),
);

$argsCategory = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'eventtype' ),
);

register_taxonomy('eventtype', array('events'), $argsCategory );
}

Everything are pretty straight forward for coders . WordPress Custom Taxonomy API gives the entire details which you need for it . For custom posts , refer the wordpress custom post api.

Code divided and explained

Initialization.

add_action( 'init', 'eventcustom_init' );

we have defined a function eventcustom_init, which will be initialized once the wordpress is loaded. Or in short this function will be called on wordpress load. Now lets check what we have written inside the function eventcustom_init.
Here in the function “function eventcustom_init()” we are defining the variable argsPost

$argsPost = array( 'public' => true, 'label' => 'Events','taxonomies'=>array('eventtype') );

In the argsPost Variable we are defining the required data like label and all as well the taxonomy(category) which we are linking to the custom post.


register_post_type( 'events', $argsPost );

This registers the events custom post, and after all you can see the events post added up in the wordpress admin side (very much like the normal posts).


$labels = array(
'name' => _x( 'Event Type', 'taxonomy general name' ),
'singular_name' => _x( 'Event Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Events Type' ),
'all_items' => __( 'All Event Type' ),
'parent_item' => __( 'Parent Events Type' ),
'parent_item_colon' => __( 'Parent Events:' ),
'edit_item' => __( 'Edit Events' ),
'update_item' => __( 'Update Event Type' ),
'add_new_item' => __( 'Add New Event Type' ),
'new_item_name' => __( 'New Event Type Name' ),
'menu_name' => __( 'Event Types' ),
);

$argsCategory = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'eventtype' ),
);

$labels hold the data with respect to which the custom taxonomies will be displayed in the wordpress admin.
After all we are registering the taxonomy using

register_taxonomy('eventtype', array('events'), $argsCategory );

where the second argument to the function register_taxonomy represents the post type  ‘events‘.

 

About the author

Linjo Joson

Linjo is a PHP developer who loves to write about online businesses and marketing ideas

Add comment

Categories