Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting

Create Custom Post Type and Custom Taxonomies in WordPress



 Everyone's blog and site have some unique feature. So definitly we need customization of posts and its categories. The customization is depending upon our blog area. For eaxmple , Movie review sites, General articles, technical assistance, and etc. How do we create it based on our need and necessary?. Here i created a custom post type and custom taxonomy for movie review theme.


For creating Movie review we need a custom post type named as 'movie_review' and custom category named as 'movie_type'. And i will show you how to create and use it form them.

1. First create Custom post type by the following code.

register_post_type( 'movie_review', array(
'labels' => array(
'name' => 'Movie Review',
'singular_name' => 'Movie Review',
'add_new' => 'Add New',
'add_new_item' => 'Add New Movie Review',
'edit' => 'Edit',
'edit_item' => 'Edit Movie Review',
'new_item' => 'New Movie Review',
'view' => 'View',
'view_item' => 'View Movie Review',
'search_items' => 'Search Movie Reviews',
'not_found' => 'No Movie Reviews found',
'not_found_in_trash' =>
'No Movie Reviews found in Trash',
'parent' => 'Parent Movie Review'
),
'public' => true,
'menu_position' => 4,
'supports' => array( 'title', 'editor', 'comments', 'thumbnail','excerpt','custom-fields','comments'), 
'taxonomies' => array( 'movie_type'), 'menu_icon' => get_bloginfo('template_url') . '/images/submit_pro.png','has_archive' => true )
);

here we created a custom post type namely 'movie review'  and ready to add posts under movie review post. Look your admin Page below the Posts column it will apear.

2. Create custom Taxonomy. for creating custom taxonomies for custom posts, we need to know two of the taxonomy functions.

register_taxonony($taxonomy, $object_type, $args) ;
register_taxonomy_for_object_type($taxonomy, $object_type);


$taxonomy variable contains the information of our newly creating taxonomy name 'movie_type' and
$object_type represents our custom post type namely 'movie_review'.

Here is the full code for custom taxonomy

register_taxonomy( 'movie_type',      array('movie_review'),  array(
   'hierarchical' => true,  'labels' => array(
                            'name' => __( 'Movie', 'mywptuts'),
                            'singular_name' => __( 'Movie Category', 'mywptuts'),
                            'search_items' =>  __( 'Search Movie', 'mywptuts'),
                            'all_items' => __( 'All Movie', 'mywptuts'),
                            'parent_item' => __( 'Parent Movie Category', 'mywptuts'),
                            'parent_item_colon' => __( 'Parent Movie Category:', 'mywptuts'),
                            'edit_item' => __( 'Edit Movie Category', 'mywptuts'),
                            'update_item' => __( 'Update Movie Category', 'mywptuts'),
                            'add_new_item' => __( 'Add New Movie Category', 'mywptuts'),
                            'new_item_name' => __( 'New Movie', 'mywptuts'),
'menu_name'         => __( 'Movie' )
                    ),
                    'show_ui' => true,
                    'query_var' => true,
'update_count_callback' => '_update_post_term_count',
                    'rewrite' => array( 'slug' =>'movie_type', 'hierarchical' => true ),
            )
    );

$movie_types = array(
'Action',
'Thriller',
'Horror',
'Comedy',
'historical'
);
if ($movie_types) foreach($movie_types as $movie_type) {
if (!get_term_by( 'slug', sanitize_title($movie_type), 'movie_type')) {
wp_insert_term($movie_type, 'movie_type');
}
}


And a single line of code can help you to avoid consequences of custom post type and custom taxonomy error.

register_taxonomy_for_object_type('movie_type','movie_review');


 for avoid minetraps.

Thats it to create custom post type and taxonomies. If you have any idea and improvements drop a comment here.

0 Responses to “Create Custom Post Type and Custom Taxonomies in WordPress”

Post a Comment

Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting