Created
August 27, 2015 19:57
-
-
Save holisticnetworking/5a4028a61c6a4f6f35b4 to your computer and use it in GitHub Desktop.
Filter posts (or CPTs) by a custom taxonomy in Admin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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