Advertisement
Guest User

Filters

a guest
Aug 27th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. // Filter by Rating
  2. add_action( 'restrict_manage_posts', 'bpl_admin_posts_filter_restrict_reviews_by_rating' );
  3. function bpl_admin_posts_filter_restrict_reviews_by_rating(){
  4.     $type = 'customer_reviews';
  5.     if (isset($_GET['post_type'])) { $type = $_GET['post_type']; }
  6.  
  7.     //only add filter to post type you want
  8.     if ('customer_reviews' == $type){
  9.         //change this to the list of values you want to show
  10.         //in 'label' => 'value' format
  11.         $r_values = array(
  12.             '5 Star Reviews' => '5',
  13.             '4 Star Reviews' => '4',
  14.             '3 Star Reviews' => '3',
  15.             '2 Star Reviews' => '2',
  16.             '1 Star Reviews' => '1',
  17.         );
  18.         echo "<select name=\"bpl_rating_value\"><option value=\"\">Rating</option>";
  19.         $current_v = isset($_GET['bpl_rating_value'])? $_GET['bpl_rating_value']:'';
  20.         foreach ($r_values as $label => $value) {
  21.             printf (
  22.                 '<option value="%s"%s>%s</option>',
  23.                 $value,
  24.                 $value == $current_v? ' selected="selected"':'',
  25.                 $label
  26.             );
  27.         }
  28.         echo "</select>";
  29.     }
  30. }
  31.  
  32. add_filter( 'parse_query', 'bpl_posts_filter_reviews_by_rating' );
  33. function bpl_posts_filter_reviews_by_rating( $query ){
  34.     global $pagenow;
  35.     $type = 'customer_reviews';
  36.     if (isset($_GET['post_type'])) { $type = $_GET['post_type']; }
  37.  
  38.     if ( 'customer_reviews' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['bpl_rating_value']) && $_GET['bpl_rating_value'] != '') {
  39.         $query->query_vars['meta_key'] = 'cust_rating';
  40.         $query->query_vars['meta_value'] = $_GET['bpl_rating_value'];
  41.     }
  42. }
  43.  
  44. // Filter by Technician
  45. add_action('restrict_manage_posts','restrict_bpl_reviews_by_tech');
  46. function restrict_bpl_reviews_by_tech() {
  47.     global $wpdb;
  48.     $type = 'customer_reviews';
  49.     if (isset($_GET['post_type'])) { $type = $_GET['post_type']; }
  50.  
  51.     if ('customer_reviews' == $type){
  52.         $techs = $wpdb->get_col("SELECT DISTINCT meta_value FROM ". $wpdb->postmeta ." WHERE meta_key = 'customer_tech' ORDER BY meta_value");
  53.         echo "<select name=\"bpl_restrict_reviews_by_tech\" id=\"review_techs\"><option value=\"\">Technician</option>";
  54.         foreach ($techs as $tech) :
  55.             $the_value = unserialize( $tech );
  56.             echo "<option value=\"",$the_value[0],"\">";
  57.             if ( isset ($_GET['bpl_restrict_reviews_by_tech']) && !empty($_GET['bpl_restrict_reviews_by_tech']) ) { selected($_GET['bpl_restrict_reviews_by_tech'], $tech); }
  58.             $tech = get_post($tech);
  59.             echo get_the_title($the_value[0]);
  60.             echo "</option>";
  61.         endforeach;
  62.         echo "</select>";
  63.     }
  64. }
  65.  
  66. add_filter( 'parse_query', 'bpl_posts_filter_reviews_by_tech' );
  67. function bpl_posts_filter_reviews_by_tech( $where ){
  68.     global $pagenow;
  69.     $type = 'customer_reviews';
  70.     if (isset($_GET['post_type'])) { $type = $_GET['post_type']; }
  71.  
  72.     if ( 'customer_reviews' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['bpl_restrict_reviews_by_tech']) && $_GET['bpl_restrict_reviews_by_tech'] != '') {
  73.         $where->query_vars['meta_key'] = 'customer_tech';
  74.         $where->query_vars['meta_value'] = $_GET['bpl_restrict_reviews_by_tech'];
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement