Skip to content

Instantly share code, notes, and snippets.

@yiannisdesp
Last active November 8, 2016 11:42
Show Gist options
  • Save yiannisdesp/1c316c8079ad6482281fb43567f9247e to your computer and use it in GitHub Desktop.
Save yiannisdesp/1c316c8079ad6482281fb43567f9247e to your computer and use it in GitHub Desktop.
function dynamically_set_filter_in_admin_list_view($functionName, $taxonomyName, $postType) {
// a quick check for the function name
if(function_exists ( $functionName ) || array_key_exists($functionName, $GLOBALS)) {
die('Function <i>' . $functionName . '</i> exists. Please name a different function name.');
return false;
}
$GLOBALS[$functionName] = function() use ($taxonomyName, $postType){
global $typenow;
if( $typenow == $postType ){
$tax_obj = get_taxonomy($taxonomyName);
$tax_name = $tax_obj->labels->name;
$terms = get_terms($taxonomyName);
echo "<select name='" . $taxonomyName . "' id='" . $taxonomyName . "' class='postform'>";
echo "<option value=''>Show All " . $tax_name . "</option>";
if(!isset($_GET[$taxonomyName])) $_GET[$taxonomyName] = '';
foreach ($terms as $term) {
echo '<option value='. $term->slug, $_GET[$taxonomyName] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
}
echo "</select>";
}
};
add_action( 'restrict_manage_posts', $GLOBALS[$functionName] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment