/**
  * Custom SQL join for orderby
  *
  * @param string   $join_sql
  * @param wp_query $query
  *
  * @return string
  */
 public static function posts_join_orderby($join_sql, $query)
 {
     global $wpdb;
     switch ($query->get('orderby')) {
         case 'venue':
             $join_sql .= " LEFT JOIN {$wpdb->postmeta} tribe_order_by_venue_meta ON {$wpdb->posts}.ID = tribe_order_by_venue_meta.post_id AND tribe_order_by_venue_meta.meta_key='_EventVenueID' LEFT JOIN {$wpdb->posts} tribe_order_by_venue ON tribe_order_by_venue_meta.meta_value = tribe_order_by_venue.ID ";
             break;
         case 'organizer':
             $join_sql .= " LEFT JOIN {$wpdb->postmeta} tribe_order_by_organizer_meta ON {$wpdb->posts}.ID = tribe_order_by_organizer_meta.post_id AND tribe_order_by_organizer_meta.meta_key='_EventOrganizerID' LEFT JOIN {$wpdb->posts} tribe_order_by_organizer ON tribe_order_by_organizer_meta.meta_value = tribe_order_by_organizer.ID ";
             break;
         default:
             break;
     }
     return apply_filters('tribe_events_query_posts_join_orderby', $join_sql);
 }
 /**
  * Custom SQL order by statement for Event Start Date result order.
  *
  * @param string  $order_sql
  * @param wp_query $query
  * @return string
  */
 public static function posts_orderby($order_sql, $query)
 {
     global $wpdb;
     if ($query->tribe_is_event || $query->tribe_is_event_category) {
         $order = !empty($query->order) ? $query->order : $query->get('order');
         $orderby = !empty($query->orderby) ? $query->orderby : $query->get('orderby');
         $order_sql = "DATE({$wpdb->postmeta}.meta_value) {$order}, TIME({$wpdb->postmeta}.meta_value) {$order}";
         switch ($orderby) {
             case 'venue':
                 $order_sql = "tribe_order_by_venue.post_title {$order}, " . $order_sql;
                 break;
             case 'organizer':
                 $order_sql = "tribe_order_by_organizer.post_title {$order}, " . $order_sql;
                 break;
             case 'title':
                 $order_sql = "{$wpdb->posts}.post_title {$order}, " . $order_sql;
                 break;
             case 'menu_order':
                 $order_sql = "{$wpdb->posts}.menu_order ASC, " . $order_sql;
                 break;
             case 'event_date':
             default:
                 // we've already setup $order_sql
                 break;
         }
     }
     return $order_sql;
 }
 /**
  * Adds a custom SQL join when ordering by venue or organizer is desired.
  *
  * @param string   $join_sql
  * @param wp_query $query
  *
  * @return string
  */
 public static function posts_join_venue_organizer($join_sql, $query)
 {
     // bail if this is not a query for event post type
     if ($query->get('post_type') !== Tribe__Events__Main::POSTTYPE) {
         return $join_sql;
     }
     global $wpdb;
     switch ($query->get('orderby')) {
         case 'venue':
             $join_sql .= " LEFT JOIN {$wpdb->postmeta} tribe_order_by_venue_meta ON {$wpdb->posts}.ID = tribe_order_by_venue_meta.post_id AND tribe_order_by_venue_meta.meta_key='_EventVenueID' LEFT JOIN {$wpdb->posts} tribe_order_by_venue ON tribe_order_by_venue_meta.meta_value = tribe_order_by_venue.ID ";
             break;
         case 'organizer':
             $join_sql .= " LEFT JOIN {$wpdb->postmeta} tribe_order_by_organizer_meta ON {$wpdb->posts}.ID = tribe_order_by_organizer_meta.post_id AND tribe_order_by_organizer_meta.meta_key='_EventOrganizerID' LEFT JOIN {$wpdb->posts} tribe_order_by_organizer ON tribe_order_by_organizer_meta.meta_value = tribe_order_by_organizer.ID ";
             break;
         default:
             return $join_sql;
             break;
     }
     /**
      * Ensures we add the matching corresponding code to the order clause.
      *
      * We use posts_clauses (as opposed to posts_orderby) in this case to avoid
      * it being overwritten by Tribe__Events__Admin_List methods.
      *
      * @see Tribe__Events__Admin_List
      */
     add_filter('posts_clauses', array(__CLASS__, 'posts_orderby_venue_organizer'), 100, 2);
     if (has_filter('tribe_events_query_posts_join_orderby')) {
         /**
          * Historically this filter has only been useful to modify the joins setup
          * in relation to organizers and venues. In future it may have a more general
          * application or may be removed.
          *
          * @deprecated since 4.0.2
          *
          * @var string $join_sql
          */
         $join_sql = apply_filters('tribe_events_query_posts_join_orderby', $join_sql);
         _doing_it_wrong('tribe_events_query_posts_join_orderby (filter)', 'To modify joins in relation to venues and organizers specifically you are encouraged to use the tribe_events_query_posts_join_venue_organizer hook.', '4.0.2');
     }
     /**
      * Provides an opportunity to modify the join condition added to the query when
      * ordering by venue or organizer.
      *
      * @var string $join_sql
      */
     return apply_filters('tribe_events_query_posts_join_venue_organizer', $join_sql);
 }
 /**
  * Custom SQL order by statement for Event Start Date result order.
  *
  * @param string  $order_sql
  * @param wp_query $query
  * @return string
  */
 public static function posts_orderby($order_sql, $query)
 {
     global $wpdb;
     if ($query->tribe_is_event || $query->tribe_is_event_category) {
         $order = !empty($query->order) ? $query->order : $query->get('order');
         $orderby = !empty($query->orderby) ? $query->orderby : $query->get('orderby');
         $order_sql = "DATE({$wpdb->postmeta}.meta_value) {$order}, TIME({$wpdb->postmeta}.meta_value) {$order}";
         do_action('log', 'orderby', 'default', $orderby);
         switch ($orderby) {
             case 'venue':
                 $order_sql = "tribe_order_by_venue.post_title {$order}, " . $order_sql;
                 break;
             case 'organizer':
                 $order_sql = "tribe_order_by_organizer.post_title {$order}, " . $order_sql;
                 break;
             case 'title':
                 $order_sql = "{$wpdb->posts}.post_title {$order}, " . $order_sql;
                 break;
             case 'menu_order':
                 $order_sql = "{$wpdb->posts}.menu_order ASC, " . $order_sql;
                 break;
             case 'event_date':
                 // we've already setup $order_sql
                 break;
         }
     } else {
         if ($query->tribe_is_multi_posttype) {
             if ($query->get('orderby') == 'date' || $query->get('orderby') == '') {
                 $order_sql = str_replace("{$wpdb->posts}.post_date", 'post_date', $order_sql);
             }
         }
     }
     $order_sql = apply_filters('tribe_events_query_posts_orderby', $order_sql, $query);
     return $order_sql;
 }