Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Seperating custom post type categories from post categories WordPress

  • SOLVED

Hi,

I've registered a custom post type 'download' like so...


// Download's Post Type

add_action( 'init', 'create_post_type' );
function create_post_type() {
$args = array(
'labels' => post_type_labels( 'Download' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'menu_icon' => 'http://www.repsolhondateam.com/wp/wp-content/plugins/download-monitor/img/menu_icon.png',
'supports' => array('title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments'
)
);

register_post_type( 'download', $args );
}

function post_type_labels( $singular, $plural = '' )
{
if( $plural == '') $plural = $singular .'s';

return array(
'name' => _x( $plural, 'post type general name' ),
'singular_name' => _x( $singular, 'post type singular name' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New '. $singular ),
'edit_item' => __( 'Edit '. $singular ),
'new_item' => __( 'New '. $singular ),
'view_item' => __( 'View '. $singular ),
'search_items' => __( 'Search '. $plural ),
'not_found' => __( 'No '. $plural .' found' ),
'not_found_in_trash' => __( 'No '. $plural .' found in Trash' ),
'parent_item_colon' => ''
);
}

add_filter('post_updated_messages', 'post_type_updated_messages');
function post_type_updated_messages( $messages ) {
global $post, $post_ID;

$messages['download'] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf( __('Download updated. <a href="%s">View Download</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Download updated.'),
/* translators: %s: date and time of the revision */
5 => isset($_GET['revision']) ? sprintf( __('Download restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Download published. <a href="%s">View Download</a>'), esc_url( get_permalink($post_ID) ) ),
7 => __('Download saved.'),
8 => sprintf( __('Download submitted. <a target="_blank" href="%s">Preview Download</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Download scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Download</a>'),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __('Download draft updated. <a target="_blank" href="%s">Preview Download</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);

return $messages;
}


This seems to work nice, but no category's are being displayed for this.

So I added this function to enable categories for my custom post type 'download'...


add_action('init', 'download_add_default_boxes');
function download_add_default_boxes() {
register_taxonomy_for_object_type('category', 'download');
}


This seemed to work, but as I was adding categories for my 'download' post type, I realized they we're also appearing in my normal post categories.

Is there a way of segregating categories for difference post types?

Person who posts working code will get the prize (if it's possible)

Thanks

Answers (5)

2011-12-19

John Cotton answers:

There is no way of separating the two uses so you need to create a custom taxonomy for your custom post type:



<?php
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_dl_taxonomy', 0 );

//create two taxonomies, genres and writers for the post type "book"
function create_dl_taxonomy()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Download Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Download Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Download Categories' ),
'all_items' => __( 'All Download Categories' ),
'parent_item' => __( 'Parent Download Category' ),
'parent_item_colon' => __( 'Parent Download Category:' ),
'edit_item' => __( 'Edit Download Category' ),
'update_item' => __( 'Update Download Category' ),
'add_new_item' => __( 'Add New Download Category' ),
'new_item_name' => __( 'New Download Category Name' ),
'menu_name' => __( 'Download Categories' ),
);

register_taxonomy('download_cats',array('dowload'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'download-cats' ),
));


}
?>


Josh Cranwell comments:

Cheers John!

2011-12-19

Julio Potier answers:

Hello

You have to register a custom taxonomy too, example here :

<?php
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );

//create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genre' ),
);

register_taxonomy('genre',array('book'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
));

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

register_taxonomy('writer','book',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'writer' ),
));
}
?>


Than you can add this new taxo to your custom post type "download" like this :
add_action( 'init', 'create_post_type' );
function create_post_type() {
$args = array(
'labels' => post_type_labels( 'Download' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'taxonomies' => array( 'genre' ),
'menu_icon' => 'http://www.repsolhondateam.com/wp/wp-content/plugins/download-monitor/img/menu_icon.png',
'supports' => array('title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments'
)
);

register_post_type( 'download', $args );
}


See you !


Josh Cranwell comments:

Thanks Julio!

2011-12-19

Ivaylo Draganov answers:

<blockquote>Is there a way of segregating categories for difference post types?</blockquote>
Yes, you should create a custom taxonomy and attach it to your new post type. Refer to examples in the Codex:
[[LINK href="http://codex.wordpress.org/Function_Reference/register_taxonomy"]]http://codex.wordpress.org/Function_Reference/register_taxonomy[[/LINK]]

There are many arguments(some of them similar to those of post type regitstration) but the basic code is one line:

<?php register_taxonomy( 'downloads_category', 'download', array( 'hierarchical' => true ) ); ?>


Josh Cranwell comments:

Cheers

2011-12-19

Navjot Singh answers:

Just add this line for adding category (It is custom taxonomy actually) to your custom post type:

register_taxonomy("category", array("download"), array("hierarchical" => true, "label" => "Download Category", "rewrite" => true));

below this line

register_post_type( 'download', $args );


Josh Cranwell comments:

Thank you!

2011-12-19

juan manuel incaurgarat answers:

here is a working example

function post_type_portfolios() {
$labels = array(
'name' => _x('Kohteet', 'post type general name'),
'singular_name' => _x('Kohteet', 'post type singular name'),
'add_new' => _x('Add New Kohteet', 'book'),
'add_new_item' => __('Add New Kohteet'),
'edit_item' => __('Edit Kohteet'),
'new_item' => __('New Kohteet'),
'view_item' => __('View Kohteet'),
'search_items' => __('Search Kohteets'),
'not_found' => __('No Kohteet found'),
'not_found_in_trash' => __('No Kohteets found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug'=>'kohteet','with_front'=>true),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor', 'thumbnail', 'excerpt'),
'menu_icon' => get_stylesheet_directory_uri().'/functions/images/sign.png',
'taxonomies' => array('portfoliosets')
);

register_post_type( 'portfolios', $args );

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

register_taxonomy(
'portfoliosets',
'kohteet',
array(
'public'=>true,
'hierarchical' => true,
'labels'=> $labels,
'query_var' => 'portfoliosets',
'show_ui' => true,
'rewrite' => array( 'slug' => 'portfoliosets', 'with_front' => false ),
)
);
}

add_action('init', 'post_type_portfolios');


Josh Cranwell comments:

Thank you!