Reputation: 151
Just wondering, is it possible to have one set of categories for multiple post types? More specifically, is it possible to use the categories for the default post type across custom posts?
If so, is it then possible to filter these categories within the loop, to only display certain post types contained within one global category?
Thanks in advance!
Upvotes: 1
Views: 3934
Reputation: 11971
Yes. When you go and Register your Custom Post Type, you can set 'taxonomies' to an array containing either/both your custom taxonomies, as well as the core 'category' and/or 'post_tag' taxonomies.
Querying these loops can be done in a standard call to WP Query
Here's an example. This is untested, but should give you an idea on how to set everything up:
functions.php
add_action( 'init', 'codex_custom_init' );
function codex_custom_init() {
$args = array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book',
'add_new' => 'Add New',
'add_new_item' => 'Add New Book',
'edit_item' => 'Edit Book',
'new_item' => 'New Book',
'all_items' => 'All Books',
'view_item' => 'View Book',
'search_items' => 'Search Books',
'not_found' => 'No books found',
'not_found_in_trash' => 'No books found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Books'
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
'taxonomies'=>array('category')
);
register_post_type( 'book', $args );
}
loop
$q = new WP_Query(array(
'post_type'=>'book',
'category_name'=>'fantasy'
));
if($q->have_posts()) : while($q->have_posts()) : $q->the_post();
the_title();the_excerpt();
endwhile;endif;
Upvotes: 3