
Restrict category drop down to a specific post type
By:
tcbarrett on
Jan 19th, 2012 | syntax:
PHP | size: 0.89 KB | hits: 108 | expires: Never
<?php
$exclusive_post_type = 'book';
$exclude_cats = array();
$nonemptycats = get_terms( 'category' );
foreach( $nonemptycats as $term ){
$term_id = $term->term_id;
$term_objects = get_objects_in_term( $term_id, 'category' );
$exclude_term = true;
foreach( $term_objects as $post_id ){
if ( $exclusive_post_type === get_post_type( $post_id ) ){
$exclude_term = false;
break;
}
}
if ( $exclude_term ) $exclude_cats[] = $term_id;
}
$args = array(
'orderby' => 'name',
'show_count' => false,
'hide_if_empty' => true,
'show_option_all' => 'All categories',
'depth' => 1,
'exclude' => $exclude_cats,
);
?>
Categories: <?php wp_dropdown_categories( $args ); ?>