Advertisement
Guest User

Event filtering

a guest
Sep 1st, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. class Custom_Events_Date_Range_Filter extends Date_Range_Filter
  2. {
  3.  
  4.     public static function setup()
  5.     {
  6.         parent::setup(); // optionnal
  7.     }
  8.  
  9.     public static function filter_main_query($wp_query)
  10.     {
  11.         global $typenow, $pagenow;
  12.         /**
  13.          * Override queries for bookings
  14.          */
  15.         if ($typenow == 'event') {
  16.             if (
  17.                 is_admin() && $wp_query->is_main_query() && in_array($pagenow,
  18.                     array('edit.php')) && !empty($_GET['date_from']) && !empty($_GET['date_to'])
  19.             ) {
  20.                 $from = explode('/', sanitize_text_field($_GET['date_from']));
  21.                 $to   = explode('/', sanitize_text_field($_GET['date_to']));
  22.  
  23.                 $from = array_map('intval', $from);
  24.                 $to   = array_map('intval', $to);
  25.  
  26.                 if (
  27.                     3 === count($to) && 3 === count($from)
  28.                 ) {
  29.                     list( $year_from, $month_from, $day_from ) = $from;
  30.                     list( $year_to, $month_to, $day_to ) = $to;
  31.                 } else {
  32.                     return $wp_query;
  33.                 }
  34.  
  35.                 $from_dt = $year_from."-".sprintf('%02d', $month_from)."-".sprintf('%02d', $day_from)." 00:00:00";
  36.                 $to_dt   = $year_to."-".sprintf('%02d', $month_to)."-".sprintf('%02d', $day_to)." 23:59:59";
  37.  
  38.                 $wp_query->set(
  39.                     'meta_query',
  40.                     array(
  41.                     array(
  42.                         'key' => 'start_date',
  43.                         'value' => array($from_dt, $to_dt),
  44.                         'compare' => 'BETWEEN',
  45.                         'type' => 'DATETIME'
  46.                     )
  47.                     )
  48.                 );
  49.             }
  50.         } else {
  51.             $wp_query = parent::filter_main_query($wp_query);
  52.         }
  53.  
  54.         return $wp_query;
  55.     }
  56. }
  57. remove_action('plugins_loaded', array('Date_Range_Filter', 'setup'));
  58. add_action('plugins_loaded', array('Custom_Events_Date_Range_Filter', 'setup'), 20);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement