My terms have an additional ACF true/false field. I need to get a list of all the term IDs with the field enabled and return a list of these IDs.
So far I have achieved such a code, but it returns me only one ID:
function terms_exclude_id_func() { $terms_control = get_terms([ 'taxonomy' => 'product_cat', 'hide_empty' => false, 'meta_query' => [ [ 'key' => 'glob_menu', 'value' => '1', ] ] ]); foreach ($terms_control as $term_control) { $IDs = $term_control->term_id; } return $IDs; } echo terms_exclude_id_func();
I need echo terms_exclude_id_func();
to return something like this 688, 534, 827
Best Answer
in your foreach loop, $IDs must be an array, and you need to push the values into it, not redefine its value:
function terms_exclude_id_func() { $terms_control = get_terms([ 'taxonomy' => 'product_cat', 'hide_empty' => false, 'meta_query' => [ [ 'key' => 'glob_menu', 'value' => '1', ] ] ]); $IDs = array(); // define the variable as an array foreach ($terms_control as $term_control) { $IDs[] = $term_control->term_id; // push to the array } return $IDs; }