SOLVED: Dynamic tag used in query not returning correct results in the builder

Hi I think I have discovered a bug with the Taxonomy query Terms field.

It will not accept dynamic data.

In my example I have a product attribute ‘Brands’ inside this are a few terms and I want to only display 1 of them in a template.

It works perfectly if I enter the term manually. In this case the term id is 120

When I use dynamic data it fails. In this example acf_brand_attribute_is is a number field, but I have tried a taxonomy field and a php function. I can echo any of these versions out and perform calculations on them - so I think the data is good. It just isn’t being pulled into Bricks.

Hi,
Thanks so much for your report!

I’m unsure how to reproduce the problem, but is it possible that the query doesn’t work in the builder but does in the front end?

If this is not the case, please send temporary login credentials and a link to this thread to help@bricksbuilder.io using the email address you used during the purchase.

Best regards,
timmse

Thanks Timmse,

I also hoped it would render on the front end, but it did not.

The site is now live and I am away from work for the next couple of days, so I will get you access to the staging site when I return.

I have what might be a similar problem. I might be wrong but In my case, I’m attempting to use the name. I create a custom Taxonomy using ACF named “country”. In this case, the query goes through If I manually type “Lebanon”. But IfI use a dynamic tag like {post_terms_country:plain} nothing happens. My next attempt was to use a php function to return the ID so I might try it in this field. That’s when I stumbled upon this post

@timmse I can grant you temp access to review the bug if it helps

Hi Oscar,
You are welcome to send access data, information on where to find the loop, and a link to this thread to help@bricksbuilder.io.

Thanks @timmse I’ve just sent an email

@timmse I am back on deck and have also sent an email.

Please let me know if you require anything more from me.

Hi @J_A ,

As per my email reply, actually, the {acf_xxx} used in the taxonomy query is working well in the actual frontend.
Unfortunately, it does not work as expected in the builder.
Already recorded this in the bug tracker.
We will tackle this issue.

Regards,
Jenn

1 Like

Thanks Jenn!

Hopefully this helps anyone that stumbles across this thread.

Hi guys,

We’ve fixed this in Bricks 1.12 BETA, now available as a manual download (Bricks – Account)

Please let us know if you are still experiencing issues.

You can see the full changelog here: Bricks 1.12 Changelog – Bricks

As with any beta release, please do not use it on a production/live website. It is only meant for testing in a local or staging environment.

Best regards,
Matej

look like this is still not working in 1.12.3 version… my loop works when hardcodwd, but when I try a dynamic value it doesn’t.
note dynamic value is a custom defined in functions.php

add_filter( 'bricks/dynamic_data/render_content', 'render_category_root_in_content', 20, 3 );
add_filter( 'bricks/frontend/render_data', 'render_category_root_in_content', 20, 2 );
function render_category_root_in_content( $content, $post, $context = 'text' ) {
    // If our tag doesn't appear in $content, bail early.
    if ( strpos( $content, '{category_root}' ) === false ) {
        return $content;
    }

    // Replace it with the actual root category value
    $value  = my_get_category_root_value();
    $output = str_replace( '{category_root}', $value, $content );

    return $output;
}

/**
 * 4) The actual logic to retrieve your category root value
 *    Put your ancestor logic here. This is so we can reuse it in both filters.
 */
function my_get_category_root_value() {
    // If not on a category archive, just return empty
    if ( ! is_category() ) {
        return '';
    }

    $current_cat = get_queried_object();
    if ( ! $current_cat || empty( $current_cat->term_id ) ) {
        return '';
    }

    $ancestors = get_ancestors( $current_cat->term_id, 'category' );
    $root_id   = ( ! empty( $ancestors ) ) ? end( $ancestors ) : $current_cat->term_id;

	return $root_id;

Hi @philooo,

sorry for the late response. Can you check if your {category_root} dynamic tag returns the correct value? You can try to output it in the basic text element, and you should see what it returns.

Also, can you give me a screenshot, or even better - a video with sound, or your query setup?

Thanks,
Matej

I was ultimatley able to get a code that works.

it is crazy to me that these type of variables like the root_category_id are NOT INCLUDED in the base brick plugin. the loop query function is super weak and this could be a killer tool if it was fully developped… too bad

my code in the end, works well. now using taxonomy filtering

/* Daixtech CATEGORY_ROOT function to get the category parent and children
   START 
*/

/**
 * 1) Register the dynamic tag so it appears in Bricks’ dropdown.
 */
add_filter( 'bricks/dynamic_tags_list', 'register_category_root_tag' );
function register_category_root_tag( $tags ) {
    $tags[] = [
        'name'  => '{category_root}',
        'label' => 'Category Root ID',
        'group' => 'Taxonomy', // or any group name you prefer
    ];
    return $tags;
}

/**
 * 2) Provide the value if user selects "{category_root}" from the dropdown.
 */
add_filter( 'bricks/dynamic_data/render_tag', 'render_category_root_tag', 20, 3 );
function render_category_root_tag( $tag, $post, $context = 'text' ) {
    if ( '{category_root}' !== $tag ) {
        return $tag; // Not our tag
    }

    // We'll just return a placeholder string for testing
    // Return "root category" to confirm it’s being called.
    return my_get_category_root_value();
}

/**
 * 3) Ensure we also replace "{category_root}" if typed directly in content.
 *    (Like how your loop_index code calls render_loop_index_in_content)
 */
add_filter( 'bricks/dynamic_data/render_content', 'render_category_root_in_content', 20, 3 );
add_filter( 'bricks/frontend/render_data', 'render_category_root_in_content', 20, 2 );
function render_category_root_in_content( $content, $post, $context = 'text' ) {
    // If our tag doesn't appear in $content, bail early.
    if ( strpos( $content, '{category_root}' ) === false ) {
        return $content;
    }

    // Replace it with the actual root category value
    $value  = my_get_category_root_value();
    $output = str_replace( '{category_root}', $value, $content );

    return $output;
}

/**
 * 4) The actual logic to retrieve your category root value
 *    Put your ancestor logic here. This is so we can reuse it in both filters.
 */
function my_get_category_root_value() {
    // If not on a category archive, just return empty
    if ( ! is_category() ) {
        return '';
    }

    $current_cat = get_queried_object();
    if ( ! $current_cat || empty( $current_cat->term_id ) ) {
        return '';
    }

    $ancestors = get_ancestors( $current_cat->term_id, 'category' );
    $root_id   = ( ! empty( $ancestors ) ) ? end( $ancestors ) : $current_cat->term_id;

	return $root_id;

   // below code to return name instead of id.
	// You could return $root_id, or get the category object for the name/slug
    // For example: return the root category NAME
    //$root_term = get_term( $root_id, 'category' );
    // return ( $root_term && ! is_wp_error( $root_term ) ) ? $root_term->name : '';
}


/* Daixtech function to get the category parent and children
   END 
*/

Hi @philooo,

I’m glad you solved it and that it’s working now.

I have a question for you though, can you explain a bit this one:

What would you expect out of the query loop, apart from the dynamic tag like you mentioned before? Help up make it better :wink:

Best reagrds,
Matej