SHARE
TWEET

Untitled

a guest Mar 13th, 2012 614 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. add_action( 'restrict_manage_posts', 'acs_admin_posts_filter_restrict_manage_posts' );
  3. /**
  4.  * First create the dropdown
  5.  * make sure to change POST_TYPE to the name of your custom post type
  6.  *
  7.  * @author Ohad Raz
  8.  *
  9.  * @return void
  10.  */
  11. function acs_admin_posts_filter_restrict_manage_posts(){
  12.     $type = 'contestentry';
  13.     if (isset($_GET['post_type'])) {
  14.         $type = $_GET['post_type'];
  15.     }
  16.  
  17.     //only add filter to post type you want
  18.     if ('POST_TYPE' == $type){
  19.         //change this to the list of values you want to show
  20.         //in 'label' => 'value' format
  21.         $values = array(
  22.                         'Cow' => 'Cow',
  23.                         'Goat' => 'Goat',
  24.                         'Sheep' => 'Sheep',
  25.                         'Cow-Goat' => 'Cow-Goat',
  26.                         'Cow-Sheep' => 'Cow-Sheep',
  27.                         'Cow-Goat-Sheep' => 'Cow-Goat-Sheep',
  28.                         'Goat-Sheep' => 'Goat-Sheep',
  29.                         'Water Buffalo' => 'Water Buffalo',
  30.                         'Mixed' => 'Mixed'
  31.         );
  32.         ?>
  33.         <select name="ADMIN_FILTER_FIELD_VALUE">
  34.         <option value=""><?php _e('Filter By ', 'acs'); ?></option>
  35.         <?php
  36.             $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
  37.             foreach ($values as $label => $value) {
  38.                 printf
  39.                     (
  40.                         '<option value="%s"%s>%s</option>',
  41.                         $value,
  42.                         $value == $current_v? ' selected="selected"':'',
  43.                         $label
  44.                     );
  45.                 }
  46.         ?>
  47.         </select>
  48.         <?php
  49.     }
  50. }
  51.  
  52.  
  53. add_filter( 'parse_query', 'acs_posts_filter' );
  54. /**
  55.  * if submitted filter by post meta
  56.  *
  57.  * make sure to change META_KEY to the actual meta key
  58.  * and POST_TYPE to the name of your custom post type
  59.  * @author Ohad Raz
  60.  * @param  (wp_query object) $query
  61.  *
  62.  * @return Void
  63.  */
  64. function acs_posts_filter( $query ){
  65.     global $pagenow;
  66.     $type = 'post';
  67.     if (isset($_GET['post_type'])) {
  68.         $type = $_GET['post_type'];
  69.     }
  70.     if ( 'POST_TYPE' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
  71.         $query->query_vars['meta_key'] = 'ecpt_sourceofmilk';
  72.         $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
  73.     }
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top