Posted: 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? |
|
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? |
|
Sure, Tony. The variable would work something like this (assuming that there were a function for get_espresso_event_status):
|
|
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. |
|
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? |
|
The EE_Event object has a few methods. get_active_status() (which is likely the one you want to use) |
|
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. |
|
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 |
|
This reply has been marked as private. | |
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:
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:
So now we have an array of datetimes for your events. You’ve stated that
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:
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. |
|
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.