I’m almost certain I’ve done this before, so perhaps a bug was introduced in v.1.9.6.1.
I have a custom function that simply counts the number of posts in the ACF relationship field for the current post. For example, I call this function in my Practice Areas template, and the function will return the number of lawyers related to that practice area. The function works well—I can echo the result of the function as a text element’s dynamic data (while troubleshooting I am outputting the count in a red box).
The problem is that when I echo the result of the function as a dynamic data comparison in a condition (show if result > 0) it always returns true, even when the function evaluates to 0.
Here’s how the condition is set up to show or hide a block:
But here, where the count of related lawyers is zero, the block is still shown even though the condition states that it should be shown only when the count is > 0.
I had a similar error on 1.9.5. The functionality simply stopped working to compare. To make it work, I deleted the widget where the conditions was and created it anew.
Thanks for the tip. I created a brand new element and applied the same condition but it still doesn’t work. I’m hoping to hear from the Bricks team about a fix for this bug.
Would you be so kind as to send temporary login credentials and a link to this thread to help@bricksbuilder.io using the email address you used during the purchase?
As a summary, Bricks is not able to parse nested dynamic tags in conditions and element attributes currently and it’s already in our bug tracker.
For example: {echo:get_related_lawyers_count({post_id})} > 0
When executing, it is just comparing {echo:get_related_lawyers_count(5267)} > 0 (Unable to parse {echo:} dynamic tag)
Workaround method to overcome this issue:
function get_related_lawyers_count() {
// Use dynamic data inside the function
$post_id = bricks_render_dynamic_data( '{post_id}' );
// Fetch the related lawyers using ACF's get_field function
$lawyers = get_field('lawyers', $post_id);
// If related practice areas are found, return their count
if ($lawyers) {
return count($lawyers);
}
// If no related practice areas found, return 0
return 0;
}
Since you just want to retrieve the query results count from the custom functions, you can use {query_results_count}