Export all posts as individual plain txt files

I wish to export all of my posts as individual plain text files. So, the format may be something like:

title.txt

 

Read More
Title: title
Pub date: date
Category: cat

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et

Is this possible? Is there a plugin or workaround to do this?

Thanks.

Related posts

Leave a Reply

4 comments

  1. Try this (you may need to bootstrap WP by loading wp-load.php, depending on where you put this code).

    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        //'posts_per_page' => -1 //uncomment this to get all posts
    );
    
    $query = new WP_Query($args);
    while ( $query->have_posts() ) : $query->the_post();
        $f = fopen(get_the_title() . '.txt', 'w');
        $content = 'Title: ' . get_the_title() . PHP_EOL;
        $content .= 'Pub date: ' . get_the_date() . PHP_EOL;
        $content .= 'Category: ';
        foreach (get_the_category() as $cat) {
            $content .= $cat->cat_name . ', ';
        }       
        $content .= PHP_EOL . PHP_EOL;
        $content .= get_the_content();
    
        fwrite($f, $content);
        fclose($f);
    endwhile;
    

    Keep in mind, if two posts have the same title, you’ll only get one text file.

  2. A cursory search doesn’t turn up any plugins that do this … but you could use the built-in exporter as an example for building out your own plugin. It’s located in /wp-admin/includes/export.php.

    Essentially, it’s a PHP page that queries the database to get all of your posts, then dumps the content into a pre-build XML template that can be imported later.

  3. It looks very much as if you wanted to switch to a static blog generator like Jekyll. The following answer is based on pdb’s answer and should be ready-to-use. My answer can deal with:

    • multiple post with the same title
    • German umlauts

    Here it is:

    <?
    require_once('wp-load.php');
    
    function getFeaturedImage($postid) {
        $featured_image = "";
        $array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
        if (count($array)>1) {
            $featured_image = $array[0];
            $featured_image = str_replace('http://martin-thoma.com/wp-content/uploads/', '', $featured_image);
        }
        return $featured_image;
    }
    
    $args = array(
        'post_type' => 'post',
        'post_status' => 'draft',
        'posts_per_page' => -1 //uncomment this to get all posts
    );
    
    $query = new WP_Query($args);
    $i=0;
    $titles = array();
    while ( $query->have_posts() ) : $query->the_post();
        $i++;
        $name = $post->post_name;
        if ($name == "") {
            $name = strtolower(get_the_title());
            $replacements = array(
                ' ' => '-',
                '#' => 'nr',
                '/' => 'or',
                'ö' => 'o',
                'ü' => 'u',
                'ä' => 'a',
                'ß' => 'ss'
            );
            $name = str_replace(array_keys($replacements), $replacements, $name);
        }
    
        if (array_key_exists($name, $titles)) {
            $name = $post->post_name."-".$post->ID;
        } else {
            $titles[$name] = true;
        }
        echo $name."<br/>";
    
        $f = fopen("export/".$name. '.md', 'w');
        $content = "---".PHP_EOL;
        $content.= "layout: post".PHP_EOL;
        $content.= "title: ".get_the_title().PHP_EOL;
        $content.= "author: ".get_the_author().PHP_EOL;
        $content.= "date: ".get_the_date('Y-m-d h:i:s').PHP_EOL;
        $content.= "categories: ".PHP_EOL;
        $cats = get_the_category();
        if ($cats) {
            foreach($cats as $category) {
                $content .= '- '.$category->name.PHP_EOL; 
            }
        }
        $content .= 'tags: ';
        $posttags = get_the_tags();
    
        if ($posttags) {
            $content .= PHP_EOL;
            foreach($posttags as $tag) {
                $content.= "- ".$tag->name.PHP_EOL; 
            }
        } else {
            $content .= "[]".PHP_EOL;
        }
        echo wp_get_attachment_image_src(get_the_post_thumbnail($post->ID));
        $content .= 'featured_image: '.getFeaturedImage($postid).PHP_EOL;
        $content .= '---'.PHP_EOL;    
        $content .= get_the_content();
        fwrite($f, $content);
        fclose($f);
    endwhile;
    echo $i." posts exported."
    ?>