Skip to content

Instantly share code, notes, and snippets.

@holisticnetworking
Created August 27, 2015 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holisticnetworking/5a4028a61c6a4f6f35b4 to your computer and use it in GitHub Desktop.
Save holisticnetworking/5a4028a61c6a4f6f35b4 to your computer and use it in GitHub Desktop.
Filter posts (or CPTs) by a custom taxonomy in Admin.
function product_category_filter() {
global $typenow;
if ($typenow == 'product') :
$args = array(
'show_option_all' => "Show All Categories",
'taxonomy' => 'product_category',
'name' => 'product_category',
'value_field' => 'slug',
'selected' => !empty( $_GET['product_category'] ) ? $_GET['product_category'] : null
);
wp_dropdown_categories($args);
endif;
}
function product_filter_request( $request ) {
if (is_admin() && $GLOBALS['PHP_SELF'] == '/wp-admin/edit.php' && isset($request['post_type']) && $request['post_type'] == 'product') {
$term = get_term( $request['product_category'], 'product_category' );
$request['term'] = $request['product_category'];
}
return $request;
}
function __construct() {
parent::__construct();
add_action( 'restrict_manage_posts', array( &$this, 'product_category_filter' ) );
add_action( 'request', array( &$this, 'product_filter_request' ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment