Query for three events, both future and past, in special order

Home Forums Calendar Products Events Calendar PRO Query for three events, both future and past, in special order

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1300468
    ite
    Participant

    Hey,

    I’m trying to make a query to fetch events like this:

    I want to get X events.
    If there are upcoming events, I want them to be displayed first, in the order: closest to todays date first, then ascending dates.
    If there are less than X upcoming events, I want past events to be displayed in the order: closest to todays date first, then desc dates.

    To make things more complicated, the events need to fulfill meta query criteria as well. This is the current query:

    $events = get_posts([
    	'post_type' => 'tribe_events',
    	'posts_per_page' => 5,
    	'eventDisplay'   => 'custom',
    	'meta_query' => [
    		'key' => 'ec_kontaktperson',
    		'value' => '"' . get_the_ID() . '"', 
    		'compare' => 'LIKE'
    	]
    ]);

    This does fetch X events, both future and past, but the order of the future events are opposite of what I want.
    How can I get the desired output?

    #1301512
    Victor
    Member

    Hi Erik!

    I’d be happy to help you with this!

    First off, I do want to note that we are fairly limited in how much we can support custom development questions like this. That said, I’d be happy to at least point you in the right direction as best I can.

    I see you are using get_posts() function to get the events, but you should take a look at our tribe_get_events() helper function that will make it a bit easier. Here’s an article that will get you started on using that function > https://theeventscalendar.com/knowledgebase/using-tribe_get_events/

    To get the desired order, you should try using the order and orderby parameters.

    I hope that helps! Let me know if any other questions.

    Best,
    Victor

    #1301564
    ite
    Participant

    Hi there,

    Thanks Victor, I came up with the following solution:

    	$upcoming_events = tribe_get_events([
    		'post_type' => 'tribe_events',
    		'posts_per_page' => 3,
    		'eventDisplay'   => 'list',
    		'meta_query' => [
                        // ...
    		]
    	]);
    	$past_events = tribe_get_events([
    		'post_type' => 'tribe_events',
    		'posts_per_page' => 3,
    		'eventDisplay'   => 'past',
    		'order' => 'DESC',
    		'meta_query' => [
                        // ...
    		]
    	]);
    
    	$events = array_slice(array_merge($upcoming_events, $past_events), 0, 3);
    • This reply was modified 8 years, 10 months ago by ite.
    #1301723
    Victor
    Member

    Hey Eric!

    That’s great! Thanks for following up and for sharing that so others can make use of it.

    Don’t hesitate to open a new thread if anything comes up! 🙂

    Cheers!
    Victor

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Query for three events, both future and past, in special order’ is closed to new replies.