Bricks Version: 1.12.4 / 2.0a
Hello Team,
I’m reporting an issue I’m facing with a custom dynamic tag I’ve created.
Issue Description:
I have implemented several custom dynamic tags in my project, and they generally work smoothly. However, one specific tag, {author_badge}
, test demo is behaving inconsistently when used within a Bricks Query Loop and fails completely when filtering is applied to the loop.
The purpose of this tag is to display a badge (an icon) next to an author’s name within a Query Loop item, based on the count of published posts of a specific custom post type (‘property’) by that author in my exemple I set 10 .
The code registers the dynamic tag and uses the bricks/dynamic_data/render_content
filter to replace the tag with HTML. The rendering logic attempts to determine the correct author ID based on whether the loop is a User Query Loop or a Post Query Loop.
I have spent considerable time (2h) trying to debug this, including reviewing the documentation and also using AI assistance tools , but I haven’t been able to pinpoint the exact cause. It’s puzzling because the tag works perfectly outside of any Query Loop context.
Expected Behavior:
The {author_badge}
tag should consistently evaluate and display the badge for each author in the Query Loop whose ‘property’ count meets the defined threshold (currently 10). This should happen reliably on initial page load and persist correctly even after the Query Loop results are filtered (e.g., via Ajax).
Current Behavior (The Bug):
- On Initial Page Load/Refresh inside : When the page containing the Query Loop loads, the badge display for items within the loop is inconsistent. It sometimes appears for the wrong authors or fails to appear for authors who meet the criteria. Refreshing the page can sometimes alter which authors are incorrectly shown with or without the badge.
- After Filtering: When I use a filter element connected to the Query Loop, and the loop reloads with filtered results, the
{author_badge}
tag completely disappears from all loop items. It seems thebricks/dynamic_data/render_content
filter is either not triggered or fails to correctly identify the loop item context during the filtered render.
Steps to Reproduce:
- Ensure you have a custom post type (e.g., ‘property’).
- Install the provided custom code (ideally in a snippet manager) to register the
{author_badge}
dynamic tag and handle its rendering. - Create a Bricks template or page.
- Add a Query Loop element. Configure it to query > Users
- Inside the Query Loop item add a Basic Text element. Insert the
{author_badge}
dynamic tag, perhaps placed after the dynamic field for the Author Name. - Add a filter element on the same page and connect it to filter the Query Loop.
- View the page on the frontend. Observe the inconsistent badge display before filtering.
- Use the filter element to filter the loop results. Observe that the
{author_badge}
tag is no longer present in any of the filtered loop items.
Code Used:
<?php
/**
* Custom Bricks Builder Dynamic Data Tag: Author Badge based on Property Count.
*
* This code defines a dynamic tag {author_badge} for Bricks Builder.
* It displays an icon badge next to an author's name if they have published
* a certain threshold of 'property' custom post types (default: 10).
*
* Place this code in your child theme's functions.php or a custom plugin.
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Get the count of 'property' posts published by a specific author, with caching.
*/
if (!function_exists('get_property_count_by_author_id')) {
function get_property_count_by_author_id($author_id) {
if (!$author_id || !is_numeric($author_id)) {
return 0;
}
$cache_key = "author_{$author_id}_property_count_property";
$count = wp_cache_get($cache_key, 'user_posts_count');
if ($count === false) {
$count = count_user_posts($author_id, 'property', true);
wp_cache_set($cache_key, $count, 'user_posts_count', HOUR_IN_SECONDS);
}
return (int) $count;
}
}
/**
* Generate HTML for the author badge based on property count threshold.
*/
if (!function_exists('get_author_badge_html')) {
function get_author_badge_html($author_id, $threshold = 10) {
if (!$author_id || !is_numeric($author_id)) {
return '';
}
$property_count = get_property_count_by_author_id($author_id);
if ($property_count >= $threshold) {
$badge_icon_html = '<i class="ion-md-checkmark-circle-outline brxe-icon"></i>';
$title_text = sprintf(esc_attr__('Author has %d+ properties', 'your-textdomain'), $threshold);
$badge_html = '<span class="author-badge" title="' . esc_attr($title_text) . '">' . $badge_icon_html . '</span>';
return $badge_html;
}
return '';
}
}
/**
* Register the custom author badge dynamic data tag with Bricks Builder.
*/
add_filter('bricks/dynamic_tags_list', function($tags) {
$tags[] = [
'name' => '{author_badge}',
'label' => esc_html__('Author Badge (10+ Properties)', 'your-textdomain'),
'group' => esc_html__('Author Data', 'your-textdomain'),
];
return $tags;
});
/**
* Handle rendering the author badge tag when it appears within element content.
*/
add_filter('bricks/dynamic_data/render_content', function($content, $post, $context) {
if (stripos($content, '{author_badge}') === false) {
return $content;
}
$author_id = 0;
$loop_object_id = apply_filters('bricks/query/loop_object_id', 0);
$loop_object_type = apply_filters('bricks/query/loop_object_type', '');
if ($loop_object_id && ($loop_object_type === 'user' || $loop_object_type === 'post')) {
if ($loop_object_type === 'user') {
$author_id = $loop_object_id;
} elseif ($loop_object_type === 'post') {
$current_loop_post = get_post($loop_object_id);
if ($current_loop_post instanceof WP_Post) {
$author_id = $current_loop_post->post_author;
}
}
}
if ($author_id === 0) {
if ($post instanceof WP_Post) {
$author_id = $post->post_author;
} else {
$queried_object = get_queried_object();
if ($queried_object && $queried_object instanceof WP_User) {
$author_id = $queried_object->ID;
} elseif (get_the_author_meta('ID')) {
$author_id = get_the_author_meta('ID');
}
}
}
if ($author_id <= 0) {
return str_ireplace('{author_badge}', '', $content);
}
$badge_html = get_author_badge_html($author_id);
$content = str_ireplace('{author_badge}', $badge_html, $content);
return $content;
}, 10, 3);
/**
* Add basic CSS for the author badge container and the icon size.
*/
add_action('wp_head', function() {
?>
<style>
.author-badge {
display: inline-block;
vertical-align: middle;
margin-left: 5px;
}
.author-badge i.brxe-icon {
font-size: 16px;
}
</style>
<?php
});
// This code should be placed in functions.php of a child theme or in a custom plugin.
// Avoid placing it in the Bricks editor or code snippet plugins for reliable execution.
?>
It seems like the issue might be related to how Bricks handles the dynamic data rendering context within Query Loops, especially during Ajax-based filtering. Any guidance or potential fixes would be greatly appreciated.
Thank you for your time and support!
//overview of post counts and target authors
//preview in archive authors and or any region of the site with this tag accompanied by its author
//preview how the tag reacts in a users loop