one term two taxonomy’s?

Anybody got any solutions for this…

I’m building an app where I have two heirchical custom taxnomies acting as custom categories. So i’ve got cat1 and cat2 taxonomy.

Read More

What I want to do is copy a term, along with its children from cat1 to cat2. Maybe something like:

set_term_taxonomy($term_id, array("cat1, "cat2"));

where the same term, along with its children, could be in more than one taxonomy. The reason for 2 taxonomies is that they are 2 different shop inventories.

Related posts

Leave a Reply

1 comment

  1. Had to write a method to do it. Here is the method… hope it helps someone else 😉

    syntax:

    copy_terms($term->term_id, "taxonomy1", "taxonomy2", 0); //will copy to root of destination
    
    
    
     /**
     * Copy a term and its descendants from one taxonomy to another.
     * Both taxonomies have to be hierarchical. Will copy across 
     * posts as well.
     *
     * @param int $term the term id to copy
     * @param string $from the taxonomy of the original term
     * @param string $to the destination for the taxonomy
     * @param int $parent the parent term_id to add the taxonomy to
     * @return type true|WP_Error
     */
    function copy_terms($term, $from, $to, $parent=0) {
    
        //check for child terms      
        $term = get_term($term, $from);
        $child_terms = get_terms($from, array(
            'hide_empty' => false,
            'parent' => $term->term_id
                ));
    
        //check for child products
        $child_prods = new WP_Query(array(
                'tax_query' => array(
                    array(
                        'taxonomy' => $from,
                        'terms' => $term->term_id
                    )
                )
            ));
    
        //work out new slug
        if($parent==0) $slug = $to."-".sanitize_title($term->name);
        else{
            $parent_term = get_term($parent, $to);
            $slug = $parent_term->slug."-".sanitize_title($term->name);
        }
    
        //add term to new taxonomy
        $parent = wp_insert_term($term->name, $to, array(
            'description' => $term->description,
            'parent' => $parent,
            'slug' => $slug
                ));
    
        if(is_wp_error($parent)) return $parent;
        $parent = get_term($parent['term_id'], $to);        
        if(is_wp_error($parent)) return $parent;
    
        //loop through folders first
        foreach($child_terms as $child){
            copy_terms($child->term_id, $from, $to, $parent->term_id);
        }
    
        //loop through products
        foreach($child_prods->posts as $child){
            wp_set_post_terms($child->ID, $parent->term_id, $to, true);
        }
        return true;