August 14, 2008
Export One WordPress Blog Category to Create a New Blog
Written by: JuanchitoI’ve searched high and low for a way to move just one blog category from an existing blog into a new blog. I found tons of ways and great documentation on how to move an entire blog to a new domain, but I couldn’t find anything that showed how to take one WordPress blog category and create a new blog using that category.
I found an old plugin/import that should have done the job, but hadn’t been kept updated with the latest version of WordPress and so it didn’t work for me. I also found a ton of people on the WordPress forums interested in doing the same thing, but no one offering an answer. The best they could offer was importing just the RSS feed for that category. Problem there is that then you lose all the comments, categories and tags for those posts. Comments being the real kicker for me.
Therefore, I decided to go it alone and figure out a way to do it using the only option given in the WordPress Export which was the user that created the post. My goal was:
-Export All the posts, categories, comments, tags for just one category of my blog
-Create a list of those posts that I could use to redirect traffic/google to the new location
There might be easier ways (and hopefully they’ll make this an option in the Import), but this worked for me. Also, I wouldn’t try this method unless you’re pretty good at find and replace, executing SQL queries and editing themes.
Overview of Process:
*Export Blog to Test Server
*Change Theme to Get list of Blog Post IDs/Posts for Category
*Change Author of Each Post ID to a new author
*Export posts for new author
*Import into new blog
*Create list of 301 Redirects on old blog
*Clean Up new blog DB
Export the entire blog using the standard WordPress export feature.
Import the blog using the standard WordPress import feature onto some test blog. I prefer using xampp or something similar for my test blogs, but it’s up to you. However, I wouldn’t suggest doing this on your production blog.
Change the index.php file for your theme (wp-content/theme/nameofyourtheme/index.php) to the following code:
< ?php $posts = query_posts('order=DESC&cat=10'); ?>
< ?php //you'll need to change the above category number to match the category you want to export ?>
< ?php //if (have_posts()) : while ( have_posts() ) : the_post(); ?>
< ?php foreach($posts as $post) : ?>
^^< ?php the_permalink(); ?>**
< ?php endforeach; ?>
-I’m sure you could add this code other pages in your theme, but index.php was easiest for me.
-This is why I suggest doing it on a test system. Since changing this index.php file on your blog will essentially ruin the look of it.
-You’ll need to set the number of posts on the main page to a high number in Settings–>Reading and change the value of “Blog pages show at most”
Change Author of Blog Posts
Now change your blog permalinks settings to the default. When you load your blog’s main page with Permalinks set to default, you’ll see a list something like the following:
^^http://localhost/testexport/?p=621**
Note: I like to add the ^^ and ** at the beginning and end to help with find and replace later.
Now we need to take this list and change it into a list of SQL commands that will change the author of every blog post in that category to a specific user that has no other posts. In my case, I created a new user called exportuser in the WordPress admin panel and then I found it’s id was 2 using phpMyAdmin (select * from wp_users)
Then, using the list from above, I could find and replace to easily create a list of update statements like the following:
UPDATE wp_posts set post_author = 2 where ID = 621;
Once you run those SQL statements in phpMyAdmin, then you can go into the WordPress Export feature and export all the posts by the “exportuser” (or whichever user you created) into a nice xml file. Now that file is an export of blog posts, comments, categories and tags from just that one category. ***Now all you have to do is import this file into your new blog.
***I personally imported this file into a separate blog so that I could clean out any unneeded categories, pages, posts, etc that I didn’t need on the new blog. Then, after I cleaned up I exported the cleaned version and then uploaded it to the new blog, but it’s up to you if your anal like me.
Redirecting Old Blog Posts URL
Next, change your blog permalink settings to the same as the original blog. When you load your blog’s main page with Permalinks set to your normal Permalinks setting, you’ll see a list something like the following:
^^http://localhost/testexport/2008/08/05/some-page/**
Note: I like to add the ^^ and ** at the beginning and end to help with find and replace later.
Now we need to convert this list into a list of 301 redirects so that posts on your old blog will forward to the new one. Using a little find and replace and pasting the columns into a csv or tab delimited file in excel, you can pretty easily produce a list of 301 redirects like the following:
RedirectPermanent /2008/08/05/some-page/ http://www.new-domain.com/2008/08/05/some-page/
**If you’re blog is in the blog folder instead of the root of your domain, you’ll need the redirect to look something like this:
RedirectPermanent /blog/2008/08/05/some-page/ http://www.new-domain.com/2008/08/05/some-page/
Copy this list of Redirects into the bottom of your .htaccess file and your old blog post urls will now redirect to your new blog.
Note: You should also consider redirecting the category itself and any relevant tags that might get traffic.
Once you have uploaded the posts to the new blog, you’ll also want to consider running the following 2 queries as suggested here:
UPDATE wp_posts SET guid = replace(guid, ‘http://www.old-domain.com’,'http://www.new-domain.com’);
This query updates the guid for the post to your new domain. Don’t ask me why the guid needs to be updated, but I tried it and it didn’t hurt anything. So, I’m suggesting you do it too. Better to leave no trace of the previous blog.
UPDATE wp_posts SET post_content = replace(post_content, ‘http://www.old-domain.com’, ‘http://www.new-domain.com’);
This one changes any links in the posts content to point to the new domain. Be careful with this one, because since you moved only one category you may not want to update ALL of the links. I’d consider using something like:
Select post_content from wp_posts where post_content like ‘%http://www.old-domain.com%’
and then editing the links manually. Or you could do the same search in the wordpress admin panel. Either way it shouldn’t matter too much since you redirected the traffic anyway.
Let me know what you think or if I missed something easy I could have done instead.
Update: I also wanted a list of all the tags that were used for that category so that I could forward most of those tags to the new site. Here’s the SQL query I used to export a list of tag slugs:
SELECT slug
FROM `wp_term_taxonomy` tax, `wp_terms` t
WHERE tax.`taxonomy` LIKE 'post_tag'
AND tax.`count` !=0
AND tax.term_id = t.term_id

[...] thanks to smuggle me for showing me a way to export a single category from one wordpress blog to another. Hopefully some [...]
I was looking for a good way of doing this and voila! an explanation from my college roommate. Juanchito, I hope everything's going well with your recent venture.
Small world. Glad that it helped. I know I looked all over when I wanted to do this and couldn't find it.