Multisite – create a category in specific site

I have a multisite I want to create a category but in a specific site. I used this functions below:

wp_create_category('mycategory', 0);

but this will create a category on the main site. I keep searching if there will be a built in function in wordpress to create a category in a specific site but it seems there is not.

Is there any easy way to create a category in a specific site?

1 Answer
1

What you need is switch_to_blog and maybe get_blog_id_from_url which you would use like this:

$blog_id = get_blog_id_from_url( 'www.example.com' );
switch_to_blog( $blog_id );
wp_create_category( 'mycategory', 0 );
restore_current_blog();

Leave a Comment