Support

Home Forums Event Espresso Premium Build an Events Loop / Query in Bricks Builder

Build an Events Loop / Query in Bricks Builder

Posted: September 19, 2023 at 9:31 am

Viewing 3 reply threads


Andrew Miller

September 19, 2023 at 9:31 am

Hello support friends,

I’m trying to combine a method I saw for creating custom Queries in Bricks Builder with Espresso’s event objects system.

While my code does display upcoming espresso events, my arguments (such as order_by, limit, etc) are not having any effect.

Here’s my php: https://gist.github.com/abmiller99/19e0e8f2d1c32c28c684c530c34817cd

Ideally I want this snippet to show upcoming events, in chronological order (Datetime.DTT_EVT_start)

Any help is appreciated. Thank you!


Brent Christensen

  • Support Staff

September 19, 2023 at 11:42 am

Hi Andrew,

I’m Event Espresso’s lead developer. Cool to see what you are working on. I have not used Data Bricks but it looks really interesting.

So in regard to your run_new_query() function:


/* Return results from our custom WP Query arguments */
function run_new_query() {

    /* Add all of your WP_Query arguments here */

    // set up show expired = false
    $where = array(
        'Datetime.DTT_EVT_end' => array( '>=', current_time( 'mysql' ))
        // 'status' => 'publish',
        // 'post_type' => 'espresso_events'
    );
    
    // run the query
    if ( class_exists( 'EE_Registry' ) ) :
    $events = EE_Registry::instance()->load_model( 'Event' )->get_all( array(
        $where,
        'limit' => 1,
        'order_by' => 'Datetime.DTT_EVT_start',
        'order' => 'ASC',
        'group_by' => 'EVT_ID'
        // 'suppress_filters' => false
    ));

    // the query
    global $wp_query;

    $posts_query = new EventEspresso\core\domain\services\wp_queries\EventListQuery( $events );

    return $posts_query->posts;

    endif;
};

the reason the query parameters added to the EEM_Event::get_all() query are not working is because you are actually not using the results of that query at all. You are taking the results of that query, which is an array of EE_Event objects, and passing it to the EventListQuery constructor, which does not accept an array of EE_Event objects. The EventListQuery constructor receives an array of attributes (typically coming from the ESPRESSO_EVENTS shortcode) which are THEN used to generate query parameters.

Here’s a couple of examples of how EventListQuery is used:

* EventEspresso\core\domain\entities\shortcodes\EspressoEvents::processShortcode()
* espresso_get_events() (found in /public/template_tags.php)

There’s two potential paths forwards for you that I can see:

1. use EE_Events directly

I’m not sure if this will work because I don’t know if Data Bricks will accept anything other than an array of WP_Posts, but you could simply try returning the results of your initial query from your run_new_query() function and NOT use EventListQuery at all.

So just try the following instead (please note that I simplified how the model is obtained):


    return EEM_Event::instance()->get_all( array(
        $where,
        'limit' => 1,
        'order_by' => 'Datetime.DTT_EVT_start',
        'order' => 'ASC',
        'group_by' => 'EVT_ID'
        // 'suppress_filters' => false
    ));

2. use EventListQuery properly

the second option, which is pretty guaranteed to work, would be to NOT use the Event model to query for events, but instead pass your query parameters to EventListQuery and let it perform the query. So get rid of the call to EEM_Event::get_all() and do something like the following instead:


    return new EventListQuery(
        [
            'limit'         => 1,
            'show_expired'  => false,
            'order_by'      => 'start_date',
            'sort'          => 'ASC',
        ]
    );

which I’m guessing should work the way you are expecting, but really is not very different than what was already happening since you were unknowingly using the defaults set up for that class. The only real difference between your query parameters above and the defaults is that you have set the limit to 1 (which means you should only see ONE event returned, not sure if that’s what you really want or not).

Now all of that said, I’m concerned by this comment you made:

While my code does display upcoming espresso events, my arguments (such as order_by, limit, etc) are not having any effect.

because again, your query parameters being passed to EEM_Event::get_all() were not far off from the defaults used by EventListQuery so you should have at least seen the correct event order. Now if you meant that changing the query parameters you passed to EEM_Event::get_all() had no effect (like setting the order to DESC), then THAT makes sense, since again, you were essentially not using the results of that query.

Anyways, I would try option 1 (use EE_Events directly) first and see if Data Bricks will accept an array of EE_Events (I’m guessing they won’t though) because it will be easier for you to manipulate that query, since you will have direct access to the query with nothing else modifying it. Check out plugins/event-espresso-core/docs/G--Model-System/model-query-params.md for more info about querying using our models.

IF that doesn’t work, then try option 2 and just use EventListQuery but that will be a little less flexible since you will be modifying a WordPress main query and will have to work within the confines of that system.

Good luck, let us know what happens

  • This reply was modified 1 year, 2 months ago by Brent Christensen. Reason: fix typos
  • This reply was modified 1 year, 2 months ago by Brent Christensen. Reason: formatting
  • This reply was modified 1 year, 2 months ago by Brent Christensen. Reason: more better formatting


Andrew Miller

September 19, 2023 at 12:53 pm

Thanks Brent, I’m having some luck with the second option, EventListQuery. It’s a simple solution, and I don’t think I’ll need anything else.

The only real difference between your query parameters above and the defaults is that you have set the limit to 1 (which means you should only see ONE event returned, not sure if that’s what you really want or not).

Just used “1” when I was testing those arguments to see if they were sticking (it wasn’t), now I know why 👊.

I have updated my full snippet here in case anyone else finds it useful https://gist.github.com/abmiller99/19e0e8f2d1c32c28c684c530c34817cd

custom query in Bricks Builder

Great support, thanks again.


Tony

  • Support Staff

September 19, 2023 at 5:17 pm

Thank you for sharing your code 🙂

Any further questions just let us know.

Viewing 3 reply threads

The support post ‘Build an Events Loop / Query in Bricks Builder’ 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