This is another fix to Donncha’s Sitewide Tags plugin that eliminates the problem of having certain categories show up as numbers in the sitewide blog in WordPress MU. In my WordPress MU installation, there was a problem with specific categories showing up as numbers for whatever reason (I figured out the reason later and went through the nightmare of permanently fixing it). When I looked at the sitewide blog tables, some of the categories were written as numbers instead of full category names and category slugs. Apparently, Donncha’s plugin relies on WordPress MU handling the category IDs that are supposed to be the same across all blogs – i.e. a category in blog 1 should have the same category ID as a category in blogs 2, as long as the category names and slugs are the same. When I looked at my installation, I found out that I had different category IDs for the same categories, due to the fact that I simply renamed some of the main tables instead of importing/exporting them when I migrated to WordPress MU from standalone WordPress installation. I certainly didn’t feel like redoing everything from scratch and looked for an alternative solution that would work for me, so here it is.
Find the line: “$post->post_category = wp_get_post_categories( $post_id );” and modify the below foreach code as shown below:
foreach( $post->post_category as $c ) {
$cat = get_category( $c );
$cats[] = array('name' => wp_specialchars($cat->name), 'slug' => wp_specialchars($cat->slug));
}
Then, locate the first “switch_to_blog( $tags_blog_id );” line and modify the code as shown below:
switch_to_blog( $tags_blog_id );
/*
* We first need to insert the categories into the new 'terms' table
* Then we need to get the ID of the category and use it while inserting the post
*/
if( is_array( $cats ) && !empty( $cats ) ) {
foreach( $cats as $t => $d ) {
/* Here is where we insert the category */
wp_insert_category( array('cat_name' => $d['name'], 'category_description' => $d['name'], 'category_nicename' => $d['slug'], 'category_parent' => '') );
/* Now get the category ID to be used for the post */
$category_id[] = $wpdb->get_var( "SELECT term_id FROM wp_" . $tags_blog_id . "_terms WHERE slug = '" . $d['slug'] . "'" );
}
}
The last change is to use the category IDs that we grabbed above while posting the entry. Locate the line that says “$post->comment_status = ‘closed’;” and insert a single line as shown below right above wp_insert_post:
/* Use the category ID in the post */
$post->post_category = $category_id;
$p = wp_insert_post( $post );
That’s it, now all categories will always have the right category name/slug in your sitewide tags blog!
P.S. The above code changes have already been applied to SiteWide tags plugin by Donncha, so make sure to get the latest version from wordpress.org.







