I am trying to display only the child terms on an archive page of a custom taxonomy’s term called “verticals”.
function filter_elementor_loop_widget_terms($query) {
// Check if it's a taxonomy archive for 'verticals'
if (is_tax('verticals')) {
// Get the current term object
$current_term = get_queried_object();
// Get child terms of the current term
$args = array(
'taxonomy' => 'verticals',
'child_of' => $current_term->term_id,
'hide_empty' => false, // Set to true if you want to hide empty terms
'fields' => 'ids', // We only need the IDs
);
$child_terms = get_terms($args);
if (!empty($child_terms) && !is_wp_error($child_terms)) {
// Modify the query to include only child terms
$query->set('post_type', 'any'); // This ensures we're adding a tax_query and not replacing it
$query->set('tax_query', array(
array(
'taxonomy' => 'verticals',
'field' => 'term_id',
'terms' => $child_terms,
),
));
} else {
// Ensure no results are returned if no child terms are found
$query->set('tax_query', array(
array(
'taxonomy' => 'verticals',
'field' => 'term_id',
'terms' => array(0), // This ensures no results
),
));
}
}
}
add_action('elementor/query/child_verticals_terms', 'filter_elementor_loop_widget_terms');
This does not display anything at all. I have tried other options as well but no luck.
I even tried giving the query manual IDs for terms to display but that does not work either.
So I am assuming there is something wrong with the way I am trying to apply this hook.
Right below this book, I created another hook to filter posts by their title and it worked fine.