Query loop orderby more than one custom field

Hi,

I have an events listing grid that shows upcoming events. On some days we have more than one event, but in this case events are in the wrong time order i.e. An event at 7pm shows before an event at 2pm.

I was advised to take a look at using bricks/posts/query_vars and have tried to implement it but with no luck.

I’m not totally sure how the bricks side of it should be set up. I have a div with ID 'event-grid-loop. This div has the ‘query loop’ setting activated with Query Type: Posts, Post type: Events, Order by: Date (default), Order: Descending (default), Posts per page: 6.

Then in my functions.php -

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    
    if( $element_id !== 'event-grid-loop') return $query_vars;

    $query_vars['meta_query'] = [
        'relation' => 'AND',
        'date' => array(
            'key' => 'date',
            'compare' => 'EXISTS',
        ),
        'time' => array(
            'key' => 'time',
            'compare' => 'EXISTS',
        ), 
    ];

    $query_vars['orderby'] = [
        'date' => 'ASC',
        'time' => 'DESC'
    ];

    return $query_vars;
}, 10, 3 );

Can’t see any change in the order of the grid (I’ve turned off cache).

Staging that I’m testing this on: Events - The Court House

and the production: https://courthousebangor.com/events/

Thanks in advance!

2 Likes

You’re using the wrong element ID. Remove your custom ID temporarily. Then you can see the „real“ (internal) element ID. Use this ID within your snippet. Of course you can readd your custom ID afterwards.

Thanks aslotta, I should’ve realised that!

Incase has trouble with the same thing here’s what’s working for me.

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    if ( $element_id !== 'gzqhhe' ) return $query_vars;

    $query_vars['meta_query'] = [
        'relation' => 'AND',
        [
            'key' => 'date',
            'value' => date('Y-m-d'),
            'compare' => '>=',
            'type' => 'DATE'
        ],
        [
            'key' => 'time',
            'compare' => 'EXISTS',
        ]
    ];

    $query_vars['orderby'] = [
        'meta_value_date' => 'ASC',
        'meta_value_time' => 'ASC'
    ];

    $query_vars['meta_query']['meta_value_date'] = [
        'key' => 'date',
        'type' => 'DATE'
    ];

    $query_vars['meta_query']['meta_value_time'] = [
        'key' => 'time',
        'type' => 'TIME',
        'compare' => 'EXISTS',
        'orderby' => 'meta_value_time',
        'meta_type' => 'TIME',
        'order' => 'ASC'
    ];

    return $query_vars;
}, 10, 3 );
1 Like