/**
 * event_espresso_core_get_events()
 *
 * Return an array of event IDs based on the parameters passed.
 *
 */
function event_espresso_core_get_events($args = '')
{
    global $bp;
    $defaults = array('type' => 'active', 'event_id' => 0, 'search_terms' => false, 'include' => false, 'per_page' => 20, 'page' => 1, 'populate_extras' => true);
    $params = wp_parse_args($args, $defaults);
    extract($params, EXTR_SKIP);
    return apply_filters('event_espresso_core_get_events', Event_Espresso_Event::get_events($type, $per_page, $page, $event_id, $include, $search_terms, $populate_extras), &$params);
}
 function get_events($type, $limit = null, $page = 1, $user_id = false, $include = false, $search_terms = false, $populate_extras = true)
 {
     global $wpdb, $bp;
     $sql = array();
     $sql['select_main'] = "SELECT DISTINCT e.id as id, e.event_code, e.event_name, e.event_desc, e.start_date, e.end_date, e.registration_start, e.registration_end, e.address, e.city, e.state, e.venue_title";
     $sql['from'] = "FROM " . EVENTS_DETAIL_TABLE . " e LEFT JOIN " . EVENTS_ATTENDEE_TABLE . " a ON a.event_id = e.id";
     $sql['where'] = 'WHERE 1=1 ';
     if ('active' == $type) {
         $sql['where_active'] = "AND e.is_active = 'Y'";
     }
     switch ($type) {
         case 'active':
         default:
             $sql[] = "ORDER BY e.submitted DESC";
             break;
     }
     if ($limit && $page) {
         $sql['pagination'] = $wpdb->prepare("LIMIT %d, %d", intval(($page - 1) * $limit), intval($limit));
     }
     /* Get paginated results */
     $paged_events_sql = apply_filters('bp_core_get_paged_users_sql', join(' ', (array) $sql), $sql);
     $paged_events = $wpdb->get_results($paged_events_sql);
     /* Re-jig the SQL so we can get the total user count */
     unset($sql['select_main']);
     if (!empty($sql['pagination'])) {
         unset($sql['pagination']);
     }
     array_unshift($sql, "SELECT COUNT(DISTINCT e.id)");
     /* Get total events results */
     $total_events_sql = apply_filters('bp_core_get_total_users_sql', join(' ', (array) $sql), $sql);
     $total_events = $wpdb->get_var($total_events_sql);
     /***
      * Lets fetch some other useful data in a separate queries, this will be faster than querying the data for every user in a list.
      * We can't add these to the main query above since only users who have this information will be returned (since the much of the data is in usermeta and won't support any type of directional join)
      */
     if ($populate_extras) {
         foreach ((array) $paged_events as $event) {
             $event_ids[] = $event->id;
         }
         $event_ids = $wpdb->escape(join(',', (array) $event_ids));
         /* Add additional data to the returned results */
         $paged_events = Event_Espresso_Event::get_event_extras(&$paged_events, $event_ids, $type);
     }
     return array('events' => $paged_events, 'total' => $total_events);
 }