Tektriks

Coding starts here

Custom Post Type with Taxonomy and Tag

Sometimes you need to make custom post type with taxonomy and tag for your project and you google it to get it. At last, you find custom post type with taxonomy everywhere but there are a few websites with custom post type with taxonomy and tag. This code is for those wordpress developers who badly need this.

Now, we are creating a custom post type named ‘book’. Write this in your theme’s functions.php file.


add_action( 'init', 'create_book_post_type' );
 
function create_book_post_type() {
    $args = array(
                  'description' => 'Post type book stores books post',
                  'show_ui' => true,
                  'menu_position' => 11, 
							    'menu_icon' => 'dashicons-book-alt', 
                  'exclude_from_search' => false,
                  'labels' => array(
                                    'name'=> 'Books',
                                    'singular_name' => 'Book',
                                    'add_new' => 'Add Book',
                                    'add_new_item' => 'Add Book',
                                    'edit' => 'Edit Book',
                                    'edit_item' => 'Edit Book',
                                    'new-item' => 'New Book',
                                    'view' => 'View Book',
                                    'view_item' => 'View Book',
                                    'search_items' => 'Search Book',
                                    'not_found' => 'No Book Found',
                                    'not_found_in_trash' => 'No Book Found in Trash',
                                    'parent' => 'Parent Book'
                                   ),
                 'public' => true,
                 'capability_type' => 'post',
                 'hierarchical' => false,
                 'rewrite' => true,
                 'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments'),
							   'has_archive' => true
                 );
    register_post_type( 'book' , $args ); // Book Post type name here.
}


add_action('init', 'create_book_taxonomy_category');
 
function create_book_taxonomy_category() {
  register_taxonomy('book_taxonomy',
                    'book',
                     array (
                           'labels' => array (
                                              'name' => 'Book Categories',
                                              'singular_name' => 'Book Categories',
                                              'search_items' => 'Search Book Categories',
                                              'popular_items' => 'Popular Book Categories',
                                              'all_items' => 'All Book Categories',
                                              'parent_item' => 'Parent Book Category',
                                              'parent_item_colon' => 'Parent Book Category:',
                                              'edit_item' => 'Edit Book Category',
                                              'update_item' => 'Update Book Category',
                                              'add_new_item' => 'Add Book Category',
                                              'new_item_name' => 'New Category',
                                            ),
                            'hierarchical' =>true,
                            'show_ui' => true,
                            'show_tagcloud' => true,
                            'rewrite' => true,
                            'public'=>true
                            )
                     );
}

 
add_filter("manage_edit-book_columns", "book_edit_columns");
 
function book_edit_columns($columns){
   $columns = array(
                    "cb" => "<input type='checkbox' />",
                    "photo" => __("Image"),
                    "title" => __("Book"),
                    "book_category" => __("Book Category"),
                    "date" => __("Date")
                   );
 
   return $columns;
}
 
 
add_action("manage_book_posts_custom_column",  "book_custom_columns");
 
function book_custom_columns($column){
  global $post;
  switch ($column){
                 case "photo":
                     if(has_post_thumbnail()) the_post_thumbnail(array(80,80));
                 break;
                 case "book_category":
                     echo get_the_term_list($post->ID, 'book_taxonomy', '', ', ','');
                 break;
   }
}



if ( isset($_GET['post_type']) ) {
   $post_type = $_GET['post_type'];
}else {
   $post_type = '';
}
 
if ( $post_type == 'book' ) { // custom post type name 
   add_action( 'restrict_manage_posts','book_filter_list' );
   add_filter( 'parse_query','check_perform_filtering' );
}
 
function book_filter_list() {
   global $typenow, $wp_query;
   if ($typenow=='book') { //// custom post type name 
      wp_dropdown_categories(array(
                                   'show_option_all' => 'Show All Book Category',
                                   'taxonomy' => 'book_taxonomy',
                                   'name' => 'book_taxonomy',
                                   'orderby' => 'name',
                                   'selected' =>( isset( $wp_query->query['book_taxonomy'] ) ? $wp_query->query['book_taxonomy'] : '' ),
                                   'hierarchical' => false,
                                   'depth' => 3,
                                   'show_count' => false,
                                   'hide_empty' => true,
                            ));
 
   }
}
 
function check_perform_filtering( $query ){
   $qv = &$query->query_vars;
   if (( $qv['book_taxonomy'] ) && is_numeric( $qv['book_taxonomy'] ) ) {
      $term = get_term_by( 'id', $qv['book_taxonomy'], 'book_taxonomy' );
      $qv['book_taxonomy'] = $term->slug;
   }
} 



 
 add_action( 'init', 'book_tag_taxonomies' ); //change order add_action( 'init', 'news_tag_taxonomies', 0 );

 function book_tag_taxonomies() {
  
  // Add new taxonomy, NOT hierarchical (like tags)
  $labels = array(
    'name' => _x( 'Book Tags', 'taxonomy general name' ),
    'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Tags' ),
    'popular_items' => __( 'Popular Tags' ),
    'all_items' => __( 'All Tags' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Tag' ), 
    'update_item' => __( 'Update Tag' ),
    'add_new_item' => __( 'Add New Tag' ),
    'new_item_name' => __( 'New Tag Name' ),
    'separate_items_with_commas' => __( 'Separate tags with commas' ),
    'add_or_remove_items' => __( 'Add or remove tags' ),
    'choose_from_most_used' => __( 'Choose from the most used tags' ),
    'menu_name' => __( 'Book Tags' ),
  ); 

  register_taxonomy('tag','book',array( // post type name here
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'tag' ),
  ));
}

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz