0

I'm attempting to setup a MultiSite network, and want the same structure across the sites. This includes Categories.

I've already got code that duplicates Posts - but discovered that if the original post was assigned a category that didn't exist on the other sites in the network,
WP would just "create" a category with the same name (but ignore description, parent etc.).

I've tried to following function - but see no creation of category on other sites in the network.

function category_to_all( $term_id, $tt_id ){

    remove_action( 'create_category', __FUNCTION__ );
    
    $currentcatdata = get_term_by('id', $id, 'category');
    
    // Get & Set Category data ready
    $categorydata = array(
        'cat_ID'               => $term_id,
        'taxonomy'             => 'category',
        'cat_name'             => $currentcatdata->name,
        'category_description' => $currentcatdata->description,
        'category_nicename'    => $currentcatdata->slug,
        'category_parent'      => $currentcatdata->parent,
    );
    
    
    
    // Get all blogs/sites, excluding "current"
    $allsiteids = get_sites(array('fields'  => 'ids'));
    $currentsiteid = get_current_blog_id();
    if (($key = array_search($currentsiteid, $allsiteids)) !== false) {
        unset($allsiteids[$key]);
    }
    $blog_ids = array_values($allsiteids);
    
    foreach( $blog_ids as $blog_id ) {
        switch_to_blog( $blog_id );
        //create category?
    
        wp_insert_term($categorydata['cat_name'], 'category', 
        array(
          'description' => $categorydata['category_description'],
          'slug' => $categorydata['category_nicename'],
          'parent'=> $categorydata['category_parent']
          )
        );
        
        restore_current_blog();
    }
    
    
}
add_action( 'create_category', 'category_to_all', 10, 2 );

The idea is that whenever anyone creates a new category,
it should be duplicated across the other sites (so new posts can go to the corresponding category per-site).

Any pointers or help would be appreciated - TY.

1 Answer 1

0

I think I figured it out.
I had multiple things wrong with the code:

function category_to_all( $term_id ){

    remove_action( 'create_category', __FUNCTION__ );
    
    $currentcatdata = get_term_by('id', $term_id, 'category');
    
    
    $allsiteids = get_sites(array('fields'  => 'ids'));
    $currentsiteid = get_current_blog_id();
    if (($key = array_search($currentsiteid, $allsiteids)) !== false) {
        unset($allsiteids[$key]);
    }
    $blog_ids = array_values($allsiteids);
    
    foreach( $blog_ids as $blog_id ) {
        switch_to_blog( $blog_id );
        
        $cid = wp_insert_term($currentcatdata->name, 'category', 
        array(
          'description' => $currentcatdata->description,
          'slug' => $currentcatdata->slug,
          'parent'=> $currentcatdata->parent,
          )
        );
        
        restore_current_blog();
    }
}
add_action( 'create_category', 'category_to_all', 10, 1 );

That seems to work, sort of - whatever blog/site your in on the multisite,
if you create a new category, it gets created on the others as well.
What's missing is handling of "parent" - if the IDs don't match, it will fail to add a new category due to mismatch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.