Skip to content

Instantly share code, notes, and snippets.

@TwisterMc
Created June 11, 2014 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TwisterMc/c5b26d7bb4fb4bdcb967 to your computer and use it in GitHub Desktop.
Save TwisterMc/c5b26d7bb4fb4bdcb967 to your computer and use it in GitHub Desktop.
Export WordPress Posts to CSV
<?php
/*
export.php - a script for outputting WordPress posts in CSV format.
Includes post ID, title, URL, tags and categories.
Tags and categories are semicolon seperated.
All commas are stripped from titles, tags and categories in order to keep the CSV format.
Drop this file in the main WordPress directory; next to wp-config.php.
Remove when done using.
*/
include "wp-load.php";
$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish');
$posts = $posts->posts;
header('Content-type:text/plain');
foreach($posts as $post) {
$permalink = get_permalink($post->ID);
$tags = '';
$categories = '';
$posttags = get_the_tags();
$postcategories = get_the_category();
if ($posttags) {
foreach($posttags as $tag) {
$tags .= $tag->name . '; ';
}
}
if ($postcategories) {
foreach($postcategories as $category) {
$categories .= $category->name . '; ';
}
}
// strip commas in names
$tags = str_replace(",", " ", $tags);
$categories = str_replace(",", " ", $categories);
$csv_title = str_replace(",", " ", $post->post_title);
echo "\n{$post->ID}, $csv_title, {$permalink}, $tags, $categories";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment