-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-filter.php
More file actions
44 lines (36 loc) · 1.29 KB
/
admin-filter.php
File metadata and controls
44 lines (36 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
/*
Plugin Name: Admin Filter
Plugin URI: https://github.com/modaresimr/admin-filter
Description: Add filterable column on admin panel
Version: 1.0.0
Author: Ali Modaresi
Author URI: https://github.com/modaresimr/
GitHub Plugin URI: https://github.com/modaresimr/admin-filter
*/
function filter_posts_by_taxonomies( $post_type, $which ) {
// A list of taxonomy slugs to filter by
$taxonomies = get_object_taxonomies($post_type,'objects');
foreach ( $taxonomies as $taxonomy_slug=>$taxonomy_obj ) {
// Retrieve taxonomy data
if(!$taxonomy_obj->show_admin_column)
continue;
$taxonomy_name = $taxonomy_obj->labels->name;
// Retrieve taxonomy terms
$terms = get_terms( $taxonomy_slug );
// Display filter HTML
echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
echo '<option value="">' . sprintf( esc_html__( 'All %s', 'admin_filter_all_text' ), $taxonomy_name ) . '</option>';
foreach ( $terms as $term ) {
printf(
'<option value="%1$s" %2$s>%3$s (%4$s)</option>',
$term->slug,
( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
$term->name,
$term->count
);
}
echo '</select>';
}
}
add_action( 'restrict_manage_posts', 'filter_posts_by_taxonomies' , 10, 2);