I wish to export all of my posts as individual plain text files. So, the format may be something like:
title.txt
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.
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.