Query Events AND Posts with get_posts and then only show upcoming events

Home Forums Calendar Products Events Calendar PRO Query Events AND Posts with get_posts and then only show upcoming events

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #110898
    Christoph
    Participant

    Hello,
    I am working on a site where I want to show the last 5 Blogposts & Events inside a slider. I query the post type “tribe_events” and “post” with get_posts and now I want to find a way of only displaying future events. Is there a smart way of doing that?

    Currently setting up $args it looks like that: http://pastebin.com/PLN8Q2td

    Thanks in advance and greetings from Berlin

    Christoph

    #111657
    Brook
    Participant

    Howdy jantanner,

    I am glad to help as best I can. Unfortunately what you want to do is not the easiest thing. You see events have a publish date like posts, and if you wanted to sort by this date things would be quite easy. However, as you clearly understand they also have start and end dates. These are separate fields, fields that do not exist for posts. You can sort by those fields rather easily using tribe_get_events, with something like the following:

    $get_events = tribe_get_events(array(
    'post_type' => array('post', 'tribe_events'),
    'start_date' => tribe_event_beginning_of_day(date('j M Y')),
    'posts_per_page'=> 5
    ), true);

    However, since post_type => ‘post’ do not have start_date, the SQL orderby ends up placing them before events in the returned posts. That’s no good. You can add a date limiter to the query, something like this:

    'date_query' => array(
    'after' => date('j M Y')
    )
    However, that will hide any events that were published before now, which is obviously no good either.

    Ultimately what it boils down to is that you are trying to sort by two columns, start_date and post_date. That is not going to be easy to do using WP_Query. You could hook into pre_get_posts and overwrite the query SQL to overwrite post_date with the post_meta _EventStartDate if it is set. That could be a bit hairy, but that is the only way I can think of to accomplish what you want.

    Does that make sense? Will that work for you? Please let me know. Cheers!

    – Brook

    #111856
    Christoph
    Participant

    Hey Brook,

    thanks for you help. Everything you say makes sense to me. I actually just found another solution to solve my issue finally:

    I set up two queries. One with get_posts for posts and the other one with tribe_get_events. I then merge the resulting two arrays and pass them through the loop. Like this I have them presented one “group” after the other but inside the same loop. I guess I could somehow reorder them if i wanted. Here is the code: http://pastebin.com/jz8UaD3b

    Do you think that is a smart way or can I run into any other trouble with this solution? On first sight it seems to work.

    …The only issue I have got left now is that for recurring events with ‘tribe_get_start_date’ it echoes the date of the first occurrence which most of the time is in the past. Do you ave any recommendation how to deal with this? As you can see in the code I query posts and events that have a certain value on their meta_key. I attribute this value to them via an ACF-Field. What is the behavior of recurring events in the Events Calendar anyways? How could I possibly target only one event out of a series of recurring events of the same kind and get that one inside the loop? That might have been answered elsewhere but I thought I just shoot it, since we are talking anyways..

    Thanks in advance!

    Christoph

    #112375
    Brook
    Participant

    Howdy jantanner,

    That’s an awesome solution! I am glad it’s going to work for you. Thanks for sharing the code.

    I notice that you are still using get_posts for $myevents. Have you tried tribe_get_events? That function handles lots of quirks relating to events that get_posts is not prepared to handle, and I believe it will resolve this issue as well. Note in our documentation for tribe_get_events that you can set the second argument to true if you need to return the full WP Query object instead of just an array of posts. Furthermore you can still do meta queries with the get events function, so that should not be an issue I do not think.

    Please let me know if that helps. Cheers!

    – Brook

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Query Events AND Posts with get_posts and then only show upcoming events’ is closed to new replies.