How To Export Articles From A Selected Category
The Wordpress CMS comes with Export/Import feature for us to export our blog posts from one website and import to another website. However, the default Export feature only allow us to export posts from a selected author.
I found one interesting request from digitalpoint forum. The user requests someone to modify the default Wordpress files so that he can exports posts from a selected category. I had tried to modify the files, found the solution and i would like to share it with my readers (I didn’t make any profit from him and someone had already sold his solution for that guy).
We need to modify the wp-admin/export.php and wp-admin/includes/export.php. Make sure you do a backup of your Wordpress database and these two files before you continue.
wp-admin/export.php
First of all, let’s start with wp-admin/export.php. Open your with wp-admin/export.php with any text editor, and search for
if ( isset( $_GET['download'] ) ) {
export_wp( $_GET['author'] );
die();
}
Replace with
if ( isset( $_GET['download'] ) ) {
export_wp( $_GET['category'] );
die();
}
Next, search for
<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 with
<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>
wp-admin/includes/export.php
Next, open your wp-admin/includes/export.php and search for
function export_wp($author='') {
Replace with
function export_wp($cat='') {
Next, search for
if ( $author and $author != 'all' ) {
$author_id = (int) $author;
$where = $wpdb->prepare(" WHERE post_author = %d ", $author_id);
}
Replace with
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).")");
}
Explanation
Basically, what you are doing is just replace the “author” with “category“. The default codes will query the available authors while my codes query the available categories.
The second file plays the most important role. It is a bit complicate to explain step by step. But basically, I will query the post ID that match the selected category ID and save the SQL query string in a $where variable. The $where string will be used in the subsequent codes, which we no need to modify.
I hope you guys can understand what i trying to explain. I had prepared a zip file that contains both of the modified files. You may download it and replace with the original files.
Note: I had tried this hack on Wordpress 2.6.2 only and i don’t know whether it works on other version or not. Please make a backup of these two files before you try!
Download: wp-admin.zip
