In addition to my question:
I also tried to add the child categories in an Array inside of the parent like:
Array ( [0] => Singles [1] => Array ( [0] => On consignment [1] => Leather [2] => Polished ) )
But then I’m getting this error:
trim() expects parameter 1 to be string, array given in
I just found a way (maybe not the best…)
First I get the parent ID using the category name
function getParentID($parent){
$r = term_exists($parent, 'portfolio_category');
return $r["term_id"];
}
Then I get all subcategories and add them to an array and check each category with a child categories list from WordPress
function getChildID($parent, $parentDescription, $child){
$allCategoriesAndSubs[] = $parentDescription; //insert parent description to array because the parent ID doesn't work
for($a = 0; $a < count($child); $a++){
$children = get_terms("portfolio_category", array('parent' => $parent,'hide_empty' => false));
$childArray = json_decode(json_encode($children), true);
$key = array_search($child[$a], array_column($childArray, 'name'));
$allCategoriesAndSubs[] = $childArray[$key]["term_id"]; //insert subs IDs
}
return $allCategoriesAndSubs;
}
Yes, in the case of redundant term names it’s best to specify term IDs instead of names. Of course if all you have are names, you’d have to query for the terms before you can get the IDs, but there’s really no need to get all children and loop through the results looking for the correct term.
By providing either “name” or “slug” args along with “parent” arg, get_terms()
will return the term object you want to add. If you also supply a 'fields'=>'ids',
arg, the function will return only matching IDs in an array, which can be passed directly to wp_set_object_terms ()
.
@spratz85 it depends… if you want to customize a theme file, you should create the child theme and duplicate whatever file you want to make these change… but it is hard to tell which one since I didn’t build your website… I’d try to hire a developer.
-
This reply was modified 1 week, 2 days ago by
bcworkz.
Important Reminder: Soliciting paid help in these forums is against our guidelines. One place for that sort of activity is jobs.wordpress.net.
Cezar, I interpreted your last reply here as work solicitation. Maybe that wasn’t your intent, but that’s how it is perceived. I’ll give you benefit of doubt this time, but please do not even hint that you’re available for hire here.
@bcworkz sure thing, you can remove that from my post since I don’t have the option to do so.
Done, but little point since the target has already seen it 🙂 Thanks for understanding.