Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'restrict_manage_posts', 'acs_admin_posts_filter_restrict_manage_posts' );
- /**
- * First create the dropdown
- * make sure to change POST_TYPE to the name of your custom post type
- *
- * @author Ohad Raz
- *
- * @return void
- */
- function acs_admin_posts_filter_restrict_manage_posts(){
- $type = 'contestentry';
- if (isset($_GET['post_type'])) {
- $type = $_GET['post_type'];
- }
- //only add filter to post type you want
- if ('contestentry' == $type){
- //change this to the list of values you want to show
- //in 'label' => 'value' format
- $values = array(
- 'Cow' => 'Cow',
- 'Goat' => 'Goat',
- 'Sheep' => 'Sheep',
- 'Cow-Goat' => 'Cow-Goat',
- 'Cow-Sheep' => 'Cow-Sheep',
- 'Cow-Goat-Sheep' => 'Cow-Goat-Sheep',
- 'Goat-Sheep' => 'Goat-Sheep',
- 'Water Buffalo' => 'Water Buffalo',
- 'Mixed' => 'Mixed'
- );
- ?>
- <select name="ADMIN_FILTER_FIELD_VALUE">
- <option value=""><?php _e('Filter By ', 'acs'); ?></option>
- <?php
- $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
- foreach ($values as $label => $value) {
- printf
- (
- '<option value="%s"%s>%s</option>',
- $value,
- $value == $current_v? ' selected="selected"':'',
- $label
- );
- }
- ?>
- </select>
- <?php
- }
- }
- add_filter( 'parse_query', 'acs_posts_filter' );
- /**
- * if submitted filter by post meta
- *
- * make sure to change META_KEY to the actual meta key
- * and POST_TYPE to the name of your custom post type
- * @author Ohad Raz
- * @param (wp_query object) $query
- *
- * @return Void
- */
- function acs_posts_filter( $query ){
- global $pagenow;
- $type = 'post';
- if (isset($_GET['post_type'])) {
- $type = $_GET['post_type'];
- }
- if ( 'contestentry' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
- $query->query_vars['meta_key'] = 'ecpt_sourceofmilk';
- $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement