Support » Developing with WordPress » Setting wp_set_object_terms to parent and child

  • Resolved Cezar Ayran

    (@ayrancd)


    I’m updating a post to the following categories:

    Singles
    – Leather
    – Polished

    But there are other categories like:

    Granite
    – Leather
    – Polished

    WordPress is adding the parent category “Singles” but the child categories are the ones under Granite… is there a way to set the array using wp_set_object_terms (or any other function) to say that Singles is the parent and Leather/Polished the child of that parent?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Cezar Ayran

    (@ayrancd)

    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

    Thread Starter Cezar Ayran

    (@ayrancd)

    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;
    }
    Moderator bcworkz

    (@bcworkz)

    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 ().

    Thread Starter Cezar Ayran

    (@ayrancd)

    @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.
    Moderator bcworkz

    (@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.

    Thread Starter Cezar Ayran

    (@ayrancd)

    @bcworkz sure thing, you can remove that from my post since I don’t have the option to do so.

    Moderator bcworkz

    (@bcworkz)

    Done, but little point since the target has already seen it 🙂 Thanks for understanding.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.