Support

Home Forums Event Espresso Premium Query to display all Events of current month

Query to display all Events of current month

Posted: November 2, 2016 at 6:03 am

Viewing 3 reply threads


yondbee

November 2, 2016 at 6:03 am

Hi i´m using EE4, i can´t figure out how to display all the events of the current month using the EE_Events_List_Query. Any help?


$start_date = date('Y-m-d');
$end_date = date('Y-m-d',strtotime("+30 days"));
$event_query = new EE_Event_List_Query(array(
   'posts_per_page'=> -1,
   'meta_key' => 'event_start_date',
   'orderby' => 'meta_value',
   'order' => 'ASC',
   'meta_query' => array(
      array(
          'key'     => 'event_start_date',
          'compare'   => 'BETWEEN',
          'value'     => array( $start_date, $end_date ),
          'type'      => 'numeric'
       ),
   ),
));


Josh

  • Support Staff

November 2, 2016 at 1:43 pm

You can remove the meta_query stuff and instead use the posts_where filter like this:

add_filter( 'posts_where', 'custom_posts_where_sql_for_only_this_month' );
function custom_posts_where_sql_for_only_this_month( $sql ) {
    $end = date('Y-m-t 12:59:59',strtotime('this month'));
    $start = date('Y-m-01 00:00:00',strtotime('this month'));
    $sql .= ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_start < "' . $end . '" ';
    $sql .= ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_start > "' . $start . '" ';
    return $sql;
}


yondbee

November 3, 2016 at 4:53 am

Thanks Josh!


Josh

  • Support Staff

November 3, 2016 at 8:29 am

You’re welcome. Would it be okay if you can post a link to the finished product when you’re done?

Viewing 3 reply threads

The support post ‘Query to display all Events of current month’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso