Issue
dev: wordpress
I got this code online, and it works great for excluding one category ID from a list... but when I try to exclude multiple categories, its only picking up the first one
function list_role_term_exclusions($exclusions,$args) {
$tax_type1 = 'category'; //string (word variable)
$cat_array = array(106, 108); //array (set) of integers (number variables)
foreach( $cat_array as $cat_id_value )
{
$children_of_parent_cat = implode(',',get_term_children($cat_id_value,$tax_type1)); // get all child categories
$children_of_parent_cat = (empty($children_of_parent_cat) ? '' : ",$children_of_parent_cat"); // if childs empty or not
return $exclusions . " AND (t.term_id NOT IN ($cat_id_value ".$children_of_parent_cat." ))"; // Exclude parent and all child cats
}
unset($cat_id_value);
}
so with this function it will successfully exclude ID 106 from categories... but not 108 (the 2nd in the array), but my goal is I want $exclusions to contain both 106 and 108 so that they can be excluded
bonus points if theres a way to assign a variable at the top like category ID = 11,12,13 tag ID = 34,25,66
so that it can exclude specific category IDs and taxonomy IDs
Solution
If you have the return
inside the foreach
, it will abort the rest of the loop, which means that you will only get the value from the first iteration.
Change your code to:
foreach( $cat_array as $cat_id_value )
{
$children_of_parent_cat = implode(',',get_term_children($cat_id_value,$tax_type1)); // get all child categories
$children_of_parent_cat = (empty($children_of_parent_cat) ? '' : ",$children_of_parent_cat"); // if childs empty or not
// Remove the return and only append the string
$exclusions .= " AND (t.term_id NOT IN ($cat_id_value ".$children_of_parent_cat." ))"; // Exclude parent and all child cats
}
return $exclusions;
Now it won't return the string until the loop is finished.
Answered By - M. Eriksson
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.