How To Export Articles From A Selected Category

You may want to read our latest article Best posts on Bloghonour from Dec 2008

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

Tags: , , , ,

You may be interested in reading these articles

6 Comments so far...

  1. rize

    can you make this work with wordpress 2.7 thanks :)

  2. dicky

    @rize -
    Do you try it with Wordpress 2.7? I should works in Wordpress 2.7, but i don’t have time to verify it.

  3. rize

    yep i tried it but it didn’t export the posts within the selected category..

  4. dicky

    @rize -
    Seems like it need some extra efforts to make it works with Wordpress 2.7. I will try it when free. Will inform you when i done.

  5. rize

    great. thanks :)

3 Trackbacks

  1. links for 2008-11-08 | benway.net
  2. links for 2008-11-08 | Savage Nomads
  3. لینک ها 2008-11-17 | Pc-aras

Leave a Reply