Support

Home Forums Event Espresso Premium espresso_event_status() in wp_query

espresso_event_status() in wp_query

Posted: June 29, 2023 at 2:12 pm

Viewing 9 reply threads


nalanda

June 29, 2023 at 2:12 pm

I am creating a wp_query loop that retrieves multiple post types, including “espresso_events”. I would like to use espresso_event_status() to filter the query so that only upcoming events are displayed. The problem is that espresso_event_status() outputs formatted code. In order to create a variable, I need to retrieve the event status as data. Is there a way for me to do this?


Tony

  • Support Staff

June 29, 2023 at 2:19 pm

Hi there,

May I ask, how you are going to use the response from espresso_event_status() to filter the above? (even if unformatted)

Do you mean to filter the results or the query itself?


nalanda

June 29, 2023 at 3:04 pm

Sure, Tony. The variable would work something like this (assuming that there were a function for get_espresso_event_status):

if( 'espresso_events' === get_post_type() ) {
  $status = get_espresso_event_status();
  if( $status != 'Expired' ) {
    // Output event details...
  }
}


Tony

  • Support Staff

June 29, 2023 at 3:46 pm

Hmm, ok so you’re not filtering the query, you’re filtering the results.

Do you need to query multiple post types at once here?

The ‘best’ option is to use our model system which makes pulling things like only upcoming events much easier, the models handle all of the joins within the queries for you but with your approach, you’ll need to handle that on your own to do this properly.

Model docs for reference: https://github.com/eventespresso/event-espresso-core/tree/master/docs/G–Model-System

To filter the results like you are doing above is going to be pretty inefficient, but doing it that way you would need use the post ID to pull in the EE_Event object and then use that to get the status. There isn’t a template tag you can use for this as is.


nalanda

June 30, 2023 at 3:37 am

Your point is taken, Tony. The goal is to create a grid for the site’s homepage that neatly displays both recent blog posts and upcoming events in the same place. It might be conceptually flawed, but I wanted to see if I could make it work.

In the model, I don’t see an object corresponding to the active/inactive status. Is there one?


Tony

  • Support Staff

June 30, 2023 at 6:37 am

The EE_Event object has a few methods.

get_active_status() (which is likely the one you want to use)
is_active()
is_upcoming()


nalanda

June 30, 2023 at 7:16 am

Thanks! Can you link me to the documentation where I can read more about those methods? The EE Event object documentation I’m referring to doesn’t seem to include them.


Tony

  • Support Staff

June 30, 2023 at 7:24 am

We don’t have documentation for every method on the models, just a general overview of how to use the models.

You can use something like Sage (previously Kint) to output the EE_Event object and view all of the method available on it easier than reading through the documentation. A quick an easy way to use Kint is using:

https://github.com/DuckDivers/kint-debugger

Install that as a plugin and wrap your event object in d();


nalanda

June 30, 2023 at 7:46 am

This reply has been marked as private.


Brent Christensen

  • Support Staff

June 30, 2023 at 1:15 pm

Hi nalanda,

I’m Event Espresso’s lead developer.

Event Espresso events are a hybrid between WordPress custom post types and our own extra meta data that is stored in its own tables. It’s important to understand that our events can have multiple datetimes assigned to them. So one event may have 2 or more datetimes which will obviously affect how you want to do things. Since it’s not entirely clear to us how exactly you want your posts displayed, i’ll just give a broad overview and you can take it from there. Our model system can do just about anything you want it to, but you have to have some half decent experience with modern PHP and general programming principles as this isn’t your typical basic WordPress stuff.

First off, you can retrieve all active and/or upcoming events with one of the following methods from core/db_models/EEM_Event.model.php:


// events that have started but not yet ended
$active_events = EEM_Event::instance()->get_active_events()

// events that have not yet started
$upcoming_events = EEM_Event::instance()->get_upcoming_events()

// everything except expired
$active_and_upcoming_events = EEM_Event::instance()->get_active_and_upcoming_events()

Now that just gets you the event objects, but again Event Espresso events can have multiple datetimes, so we need to get those, but depending on how you want to display things will change what you do in code.

You specifically stated you wanted “upcoming” events, so let’s get the upcoming events and then get the datetimes for each event:


// you can add additional query params to this array if you want
$query_params = []; 
$upcoming_events = EEM_Event::instance()->get_upcoming_events($query_params);

$event_datetimes= [];
// set limit for number of datetimes to get per event,
// setting limit to 1 will only get the next upcoming datetime for each event
// setting limit to null will return all upcoming datetimes for each event
$limit = 1;
$show_expired = false;
$show_deleted = false;
foreach ($upcoming_events as $event) {
    $event_datetimes += $event->datetimes_ordered($show_expired, $show_deleted, $limit);
}

So now we have an array of datetimes for your events. You’ve stated that
you want to display recent blog posts and upcoming events together so let’s combine those into one array and then use a custom sorting algorithm to put them in some kind of chronological order, maybe something like:


$wp_posts = get_posts(); // or whatever other query you want to run

// combine the arrays
$event_dates_and_posts = array_merge($wp_posts, $event_datetimes);

// and sort that array by date
usort(
    $event_dates_and_posts,
    function ($a, $b) {
        $start_a = $a instanceof EE_Datetime ? $a->start_date() : $a->post_date;
        $start_b = $b instanceof EE_Datetime ? $b->start_date() : $b->post_date;
        // convert to timestamps
        $start_a = strtotime($start_a);
        $start_b = strtotime($start_b);
        // compare timestamps
        if ($start_a == $start_b) {
            return 0;
        }
        return ($start_a < $start_b) ? -1 : 1;
    }
);

now we have a sorted array of WP Posts and EE event dates, lets loop through that and grab the data we want from each, maybe something like:


// now we can loop through all our posts and display them
foreach ($event_dates_and_posts as $post) {
    if ($post instanceof WP_Post) {
        // display WP post
        $title = $post->post_title;
        $start = $post->post_date;
        // etc
    }
    if ($post instanceof EE_Datetime) {
        $start = $post->start_date_and_time();
        // get the event for this datetime
        $event = $post->event();
        // verify and display EE event
        if ($event instanceof EE_Event) {
            // display EE_Event
            $title = $event->name();
            // etc
        }
    }

    echo "
    <div style='border: 1px solid #ccc; padding: 10px; margin: 10px;'>
    <h2>$title</h2>
    date: $start
    </div>";
}

I have not tested the above code, so you may need to correct a missing semicolon or something, but it looks like it should work.

Let us know how that goes.

Viewing 9 reply threads

The support post ‘espresso_event_status() in wp_query’ 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