Support

Home Forums Event Espresso Premium Query only where the first datetime is on or after today

Query only where the first datetime is on or after today

Posted: September 26, 2015 at 3:16 pm

Viewing 5 reply threads


Ruben

September 26, 2015 at 3:16 pm

I currently use the following lines to query upcoming events:

$atts = array(
	'title' => NULL,
	'limit' => $featured_num,
	'css_class' => NULL,
	'show_expired' => FALSE,
	'month' => NULL,
	'category_slug' => NULL,
	'order_by' => 'start_date',
	'sort' => 'ASC'
);

$query = new EE_Event_List_Query( $atts );

Most events have several datetimes, but I wish to only show events where the first datetime hasn’t passed. The above returns all events with any datetime in the future.

I can’t find a listing of the possible criteria.

What I’ve tried for example is to add:
‘DTT_is_primary’ => TRUE

And I wouldn’t know how to implement $post->EE_Event->first_datetime(); into this query.

Hope someone can help me out with this problem. Thanks!


Lorenzo Orlando Caum

  • Support Staff

September 29, 2015 at 8:32 am

Hi there Ruben,

Here are some resources to our developer documentation:

http://developer.eventespresso.com/docs/using-ee4-model-objects/

http://developer.eventespresso.com/docs/model-querying/

Here is an example of how to get the information that you are looking for:

$events = EEM_Event::instance()->get_all(
    array(
        array(
            'AND' => array( 'Datetime.DTT_order' => 1, 'Datetime.DTT_EVT_end' => array( '>', time() ) )
        ),
        'order_by' => array( 'Datetime.DTT_EVT_start' => 'DESC' )
    )
);


Lorenzo


Ruben

September 30, 2015 at 5:16 am

That code indeed returns the correct events. Thanks! One step remains..

The code I use relies on post objects, such as `EE_Event_List_Query’ returns.

Is there a way to convert the array returned by your code into an array of the related post objects?

Or perhaps query the required attributes found in this page using EE_Event_List_Query?


Josh

  • Support Staff

October 1, 2015 at 12:52 pm

Hi Ruben,

You should be able to adapt the above to modify your EE_Event_List_Query() with a function like this:

/* only show events where the first datetime hasn’t passed */
function custom_posts_where_sql_for_only_not_expired() {
	$dttable = EEM_Datetime::instance()->table();
	return ' AND ' . $dttable . '.DTT_order = 1 AND ' . $dttable . '.DTT_EVT_end > ' . time() . ' ';
}

the right before you do the query, you call your function on the posts_where filter like this:

add_filter( 'posts_where', 'custom_posts_where_sql_for_only_not_expired' );
$query = new EE_Event_List_Query( $atts );


Ruben

October 5, 2015 at 2:53 pm

It works like a charm!

It doesn’t seem intuitive to me, so I’ll further read into filtering posts using SQL, as I also need to have this working for the events listing shortcode.

Thanks a lot for your help!


Josh

  • Support Staff

October 6, 2015 at 11:56 am

Here’s a great resource that will help:

https://pippinsplugins.com/look-posts_where-filter/

You can also review this code as well:

https://github.com/eventespresso/ee-code-snippet-library/blob/master/templates/de_ee_filter_events_out_with_expired_tickets.php

While it doesn’t filter out events based on the first datetime, it’s similar in the way it uses the posts_where filter to exclude events that have tickets that are no longer for sale.

Viewing 5 reply threads

The support post ‘Query only where the first datetime is on or after today’ 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