7th Nov '22
/
2 comments

How to apply a custom query var to multiple query loops in Bricks

In this tutorial, we’ll learn how to apply a PHP filter to modify the query var of multiple query loops in a single function

Table of Contents

Introduction

In my last project, I needed to create multiple query loops with a custom query var that couldn’t be achieved within the builder: I needed to order the query loop by multiple dynamic values. So, instead of creating a filter for each element, I shared the same custom query var option with multiple elements.

Let’s see how to achieve it.

The code

In my case, I wanted to order the query loops by two different custom fields value: one called featured and the other one called order. In bricks, you are only able to order your loop by one single dynamic data inside the builder. So, I needed to use the bricks/posts/query_vars filter (here is the documentation) to achieve my goal.

Target ID’s

First of all, I created an array with all the IDs of the query loop elements that I wanted to modify.

Then I checked if the $element_id is part of the array, else the function stops.

And finally, I applied my custom meta_query and orderby options to the query var.

Now all the query loops listed in my array inherit from my custom query var in a single function.

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {

   // list all the elements' IDs here
   $elements = array(
       'ovkyjc',
       'flmjqp',
       'wyxffa',
       'almdys',
   );

   // Check if the element_id is inside the array
   if ( !in_array($element_id, $elements)) {
      return $query_vars;
   }

   // Apply the custom query var
   $query_vars['meta_query'] = array(
      'relation' => 'AND',
         'featured_clause' => array(
             'key' => 'featured',
             'compare' => 'EXISTS',
             'type'    => 'numeric',
         ),
         'order_clause' => array(
             'key' => 'order',
             'compare' => 'EXISTS',
             'type'    => 'numeric',
         ),
   );
   $query_vars['orderby'] = array(
      'featured_clause' => 'DESC',
      'order_clause' => 'DESC',
   );

   // return the modified query var
   return $query_vars;
}, 10, 3 );

Target a class

We can also target classes instead of IDs.

Update for Bricks v1.8+

It looks like the _cssClasses is no longer included in the $settings, so is no longer possible to use classes like this to identify elements as in the code below. Use $element_id as shown above – David.

Set a class on the query loop element. In this example we use the class my-special-class.

Then we set the variable $class with our class in the following code:

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {

   // Set your class here
   $class = 'my-special-class';
   
   // Check if the element has the correct class set
   if ( !isset( $settings['_cssClasses'] ) || !$settings['_cssClasses'] === $class ) {
      return $query_vars;
   }

   // Apply the custom query var
   $query_vars['meta_query'] = array(
      'relation' => 'AND',
         'featured_clause' => array(
             'key' => 'featured',
             'compare' => 'EXISTS',
             'type'    => 'numeric',
         ),
         'order_clause' => array(
             'key' => 'order',
             'compare' => 'EXISTS',
             'type'    => 'numeric',
         ),
   );
   $query_vars['orderby'] = array(
      'featured_clause' => 'DESC',
      'order_clause' => 'DESC',
   );

   // return the modified query var
   return $query_vars;
}, 10, 3 );

And then we’re done.

Get access to all 630 Bricks code tutorials with BricksLabs Pro

2 comments

  • Looks like this code doesn't work for Bricks 1.8. It might need an update, unless I'm doing something wrong.

    I logged the $settings variable and it doesn't have the class. I wanted to leave a comment with code but Patchstack blocked it.

    • A
      David Browne

      Can confirm, looks like we can't target elements by class. Tested with Bricks v1.8.1. The internal ID of the class is there, but not the actual class name that we add.

      Targeting by the $element_id is the way to go.

Leave a Reply to Ian (Cancel Reply)

 

Related Tutorials..

Pro
Posts Grouped by Month and Year in Bricks

Posts Grouped by Month and Year in Bricks

Nesting query loops with months & years in descending order, with the inner loop outputting the posts.
Categories:
Pro
Post Titles and Post Content Tabs in Bricks

Post Titles and Post Content Tabs in Bricks

How to set up Nestable tabs with post titles as the tab menu, and post content and/or any other post-specific data as the tab content.
Categories:
Pro
Adding active class to the current term/post in a Bricks query loop on Term archives/single posts

Adding active class to the current term/post in a Bricks query loop on Term archives/single posts

Updated on 29 Feb 2024 In the Sibling Terms Bricks Query Loop tutorial, a member asked: How can I set the class "active" to the…
Categories:
Pro
6 Random Posts from Tag A + 6 Random Posts from Tag B in a Single Bricks Query Loop

6 Random Posts from Tag A + 6 Random Posts from Tag B in a Single Bricks Query Loop

Displaying two set of random posts, inside one query loop.
Categories:
Pro
Condition to Check if the Current Category Has At Least One Child

Condition to Check if the Current Category Has At Least One Child

Looking to render an element in the category archive Bricks template only if the category of the current category archive page is a parent? This…
Categories:
Pro
Posts from Random Categories in Bricks

Posts from Random Categories in Bricks

Updated on 31 Jul 2023 In BricksLabs Facebook group a user asked: How would you query 3 WordPress posts from 3 different categories with Brick…
Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Using the core WordPress has_term() function for checking if the current post has a specific term of a given taxonomy.
Categories:
Bricks Templates Query Loop

Bricks Templates Query Loop

How display Bricks Templates with saved screenshots in a query loop.
Categories: