How To Export One Or More Categories
Well it took most of the day but I finally finished my little project. For a long period of time I've slowly been shifting another site's focus from politics to WordPress. While I still enjoy and follow politics to a degree I have found writing about WordPress and giving back to the community a much more enjoyable and rewarding experience. It's a good feeling to know the little bits of information I dispense may help someone who is stuck. However, before I could actually complete the metamorphosis I needed to remove all material of a political nature. I just didn't think it was proper that on one hand I would be blogging about a code snippet while on the other ranting about health care or the judicial system or God knows what else.
This meant I had a choice to make. I could permanently delete all of my political material, something I really didn't want to do as that would have meant putting on ice nearly five years worth of work or I could simply relocate those categories elsewhere. In the end I decided to set up a subdomain and move it all over there. Of course this lead to another dilemma. How would I do that? I mean how would I move nearly 1000 posts contained within 2 categories? WordPress has a built in export feature but that is intended for exporting and importing entire blogs - not specific categories. The same goes for the SQL dump. I certainly wasn't about to do it one by one as that could take days. I needed a way to export those posts contained within 2 categories only. I searched high and low for a plugin but what I found was unable to do the job I wanted. In the end I finally decided to examine the core files to see exactly how this export function works and you know what? I found the solution. And I'll share it with you.
This is intended only for people who wish to export one or more categories rather than a complete blog. Before going any further, I strongly suggest you create a backup just in case something goes wrong. Okay here we go.
There are 2 core files that need to be modified - one is located at wp-admin/export.php and the other wp-admin/includes/export.php Don't worry as the modifications are short and straightforward. First open wp-admin/export.php in a plain text editor such as Notepad and at about Line 16 look for the following code snippet,
if ( isset( $_GET['download'] ) ) {
export_wp( $_GET['author'] );
die();
}
and replace it with the following,
if ( isset( $_GET['download'] ) ) {
export_wp( $_GET['category'] );
die();
}
Next, in the same file at about Line 35 you'll see the following,
<table class="form-table">
<tr>
<th><label for="author"><?php _e('Restrict Author'); ?></label></th>
<td>
<select name="author" id="author">
<option value="all" selected="selected"><?php _e('All Authors'); ?></option>
<?php
$authors = $wpdb->get_col( "SELECT post_author FROM $wpdb->posts GROUP BY post_author" );
foreach ( $authors as $id ) {
$o = get_userdata( $id );
echo "<option value='$o->ID'>$o->display_name</option>";
}
?>
</select>
</td>
</tr>
</table>
Replace it with this,
<table class="form-table">
<tr>
<th><label for="category"><?php _e('Restrict Category'); ?></label></th>
<td>
<select name="category" id="category">
<option value="all" selected="selected"><?php _e('All Categories'); ?></option>
<?php
$categories = (array) get_categories('get=all');
foreach ( $categories as $c ) {
echo "<option value='$c->term_id'>$c->cat_name</option>";
}
?>
</select>
</td>
</tr>
</table>
You can now save that file and put it away. Next open up wp-admin/includes/export.php and at about Line 26 you'll see this,
function export_wp($author='') {
Replace that with the following,
function export_wp($cat='') {
This is the final step. In that same file at about Line 38 look for this,
if ( $author and $author != 'all' ) {
$author_id = (int) $author;
$where = $wpdb->prepare(" WHERE post_author = %d ", $author_id);
}
and replace it with this,
if ( $cat and $cat != 'all' ) {
$cat_id = $cat;
$ids = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id=$cat");
$where = $wpdb->prepare(" WHERE ID IN(".join(',',$ids).")");
}
Save the file and put it away. That's it. In a nutshell, what we have done is simply swap out the author for the category. It's a bit more complex than that but you get the idea. If you have done everything correctly the image below is what you should see when you browse to Tools > Export.

Test everything to make sure it works. After you have finished you might want to re-upload fresh copies of those 2 files. If you don't and you ever need to export your entire blog you won't be able to.

