- Navigate to the ch4-book-reviews folder of the WordPress plugin directory of your development installation.
- Open the ch4-book-reviews.php file in a code editor.
- Add the following line of code after the existing functions to register a function to be called when WordPress is preparing the filter drop-down boxes for the post listings:
add_action( 'restrict_manage_posts',
'ch4_br_book_type_filter_list' );
- Add the following code section to provide an implementation for the ch4_br_book_type_filter_list function:
function ch4_br_book_type_filter_list() {
$screen = get_current_screen();
global $wp_query;
if ( 'book_reviews' == $screen->post_type ) {
wp_dropdown_categories( array(
'show_option_all' => 'Show All Book Types',
'taxonomy' => 'book_reviews_book_type',
'name' => 'book_reviews_book_type',
'orderby' => 'name',
'selected' =>
( isset( $wp_query->query['book_reviews_book_type'] )
? $wp_query->query['book_reviews_book_type'] : '' ),
'hierarchical' => false,
'depth' => 3,
'show_count' => false,
'hide_empty' => true,
) );
}
}
- Insert the following line of code to register a function that will be called when the post display query is being prepared:
add_filter( 'parse_query', 'ch4_br_perform_book_type_filtering' );
- Implement the ch4_br_perform_book_type_filtering function with the following code segment:
function ch4_br_perform_book_type_filtering( $query ) {
$qv = &$query->query_vars;
if ( isset( $qv['book_reviews_book_type'] ) &&
!empty( $qv['book_reviews_book_type'] ) &&
is_numeric( $qv['book_reviews_book_type'] ) ) {
$term = get_term_by( 'id',
$qv['book_reviews_book_type'],
'book_reviews_book_type' );
$qv['book_reviews_book_type'] = $term->slug;
}
}
- Save and close the plugin file.
- Visit the Book Reviews listings to see the new dropdown to restrict what book types are displayed: