Home › Forums › Calendar Products › Events Calendar PRO › Query for three events, both future and past, in special order
- This topic has 4 replies, 2 voices, and was last updated 8 years, 10 months ago by
ite.
-
AuthorPosts
-
June 20, 2017 at 7:00 am #1300468
ite
ParticipantHey,
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?June 21, 2017 at 9:16 pm #1301512Victor
MemberHi 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,
VictorJune 22, 2017 at 1:11 am #1301564ite
ParticipantHi 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.
June 22, 2017 at 8:20 am #1301723Victor
MemberHey 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 -
This reply was modified 8 years, 10 months ago by
-
AuthorPosts
- The topic ‘Query for three events, both future and past, in special order’ is closed to new replies.
