SHOW:
|
|
- or go back to the newest paste.
1 | add_action( 'restrict_manage_posts', 'acs_admin_posts_filter_restrict_manage_posts' ); | |
2 | /** | |
3 | * First create the dropdown | |
4 | * make sure to change POST_TYPE to the name of your custom post type | |
5 | * | |
6 | * @author Ohad Raz | |
7 | * | |
8 | * @return void | |
9 | */ | |
10 | function acs_admin_posts_filter_restrict_manage_posts(){ | |
11 | $type = 'contestentry'; | |
12 | if (isset($_GET['post_type'])) { | |
13 | $type = $_GET['post_type']; | |
14 | } | |
15 | ||
16 | //only add filter to post type you want | |
17 | if ('contestentry' == $type){ | |
18 | - | if ('POST_TYPE' == $type){ |
18 | + | |
19 | //in 'label' => 'value' format | |
20 | $values = array( | |
21 | 'Cow' => 'Cow', | |
22 | 'Goat' => 'Goat', | |
23 | 'Sheep' => 'Sheep', | |
24 | 'Cow-Goat' => 'Cow-Goat', | |
25 | 'Cow-Sheep' => 'Cow-Sheep', | |
26 | 'Cow-Goat-Sheep' => 'Cow-Goat-Sheep', | |
27 | 'Goat-Sheep' => 'Goat-Sheep', | |
28 | 'Water Buffalo' => 'Water Buffalo', | |
29 | 'Mixed' => 'Mixed' | |
30 | ); | |
31 | ?> | |
32 | <select name="ADMIN_FILTER_FIELD_VALUE"> | |
33 | <option value=""><?php _e('Filter By ', 'acs'); ?></option> | |
34 | <?php | |
35 | $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:''; | |
36 | foreach ($values as $label => $value) { | |
37 | printf | |
38 | ( | |
39 | '<option value="%s"%s>%s</option>', | |
40 | $value, | |
41 | $value == $current_v? ' selected="selected"':'', | |
42 | $label | |
43 | ); | |
44 | } | |
45 | ?> | |
46 | </select> | |
47 | <?php | |
48 | } | |
49 | } | |
50 | ||
51 | ||
52 | add_filter( 'parse_query', 'acs_posts_filter' ); | |
53 | /** | |
54 | * if submitted filter by post meta | |
55 | * | |
56 | * make sure to change META_KEY to the actual meta key | |
57 | * and POST_TYPE to the name of your custom post type | |
58 | * @author Ohad Raz | |
59 | * @param (wp_query object) $query | |
60 | * | |
61 | * @return Void | |
62 | */ | |
63 | function acs_posts_filter( $query ){ | |
64 | global $pagenow; | |
65 | $type = 'post'; | |
66 | if (isset($_GET['post_type'])) { | |
67 | $type = $_GET['post_type']; | |
68 | } | |
69 | if ( 'contestentry' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') { | |
70 | - | if ( 'POST_TYPE' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') { |
70 | + | |
71 | $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE']; | |
72 | } | |
73 | } |