In today’s post, we’re going to dive into a powerful feature of WordPress and Bricks Builder: the ability to customize the display of posts based on a user’s role, using PHP filters. This can be particularly useful for websites that have content specifically for certain user groups.
Let’s start by looking at a piece of code that accomplishes this:
<?php
add_filter(
"bricks/posts/query_vars",
function ($query_vars, $settings, $element_id) {
// List all the elements' IDs here
$elements = ["26fd1c"];
// Check if the element_id is inside the array
if (!in_array($element_id, $elements)) {
return $query_vars;
}
// Get the current user's group membership
$user = wp_get_current_user();
$group_membership = "public"; // Default to public
// Check if the user is logged in and has the "private" group membership
if ($user->ID && in_array("private", $user->roles)) {
$group_membership = ["public", "private"];
}
// Apply the custom query vars using taxonomies
$query_vars["tax_query"] = [
"relation" => "AND",
[
"taxonomy" => "category",
"field" => "name",
"terms" => $group_membership,
],
];
$query_vars["orderby"] = "date";
$query_vars["order"] = "DESC";
// Return the modified query vars
return $query_vars;
},
10,
3
);
This code might seem complex at first glance, but let’s break it down to understand what’s happening.
Hooking into the Bricks Builder Filter
The add_filter
function is used to hook a function to a specific filter action. In this case, we’re hooking into the “bricks/posts/query_vars” filter. This filter allows us to modify the query variables for a specific query loop in Bricks Builder. The function we’re hooking to the filter takes three parameters: $query_vars
, $settings
, and $element_id
.
Defining the Elements
Within the function, we first define an array $elements
that contains the IDs of the elements we want to apply our custom query to. If the ID of the current element is not in this array, we return the original $query_vars
array without making any changes.
Determining User Group Membership
Next, we get the current user using the wp_get_current_user
function and set a default group membership to “public”. If the user is logged in and has the “private” role, we change the $group_membership
variable to include both “public” and “private”.
Modifying the Query Variables
Then, we modify the $query_vars
array to include our custom taxonomy query. We’re setting the taxonomy to “category”, the field to “name”, and the terms to our $group_membership
variable. We’re also setting the order of the posts to be by date and in descending order.
Returning the Modified Query Variables
Finally, we return the modified $query_vars
array. This array will now be used by Bricks Builder to display posts based on the user’s group membership.
Conclusion
This PHP filter allows us to create a custom query loop in Bricks Builder based on the user’s group membership. It’s a powerful tool that can be used to customize the display of posts in a variety of ways. With a little bit of PHP knowledge and an understanding of how WordPress filters work, you can use this technique to create your own custom query loops. Whether you’re building a membership site, a blog with premium content, or any other type of site that requires user-specific content, this technique can be a game-changer. Happy coding!