Skip to content

Instantly share code, notes, and snippets.

@chadothompson
Created July 24, 2013 14:33
Show Gist options
  • Save chadothompson/6071116 to your computer and use it in GitHub Desktop.
Save chadothompson/6071116 to your computer and use it in GitHub Desktop.
Exporting WordPress post types, titles and URLs in a CSV format.
<?php
/*
export.php - a script for outputting post type, title and URL in CSV format.
All commas are also stripped from titles in order to keep the CSV format (for HootSuite import) versus keeping the titles as is.
*/
include "wp-load.php";
$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish');
$posts = $posts->posts;
/*
global $wpdb;
$posts = $wpdb->get_results("
SELECT ID,post_type,post_title
FROM {$wpdb->posts}
WHERE post_status<>'auto-draft' AND post_type NOT IN ('revision','nav_menu_item')
");
*/
header('Content-type:text/plain');
foreach($posts as $post) {
switch ($post->post_type) {
case 'revision':
case 'nav_menu_item':
break;
case 'page':
$permalink = get_page_link($post->ID);
break;
case 'post':
$permalink = get_permalink($post->ID);
break;
case 'attachment':
$permalink = get_attachment_link($post->ID);
break;
default:
$permalink = get_post_permalink($post->ID);
break;
}
$csv_title = str_replace(",", " ", $post->post_title);
echo "\n{$post->post_type}, $csv_title ,{$permalink}}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment