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
*/