[wp-hackers] Getting every page ID, URL, and Title on site

Mike Schinkel mikeschinkel at newclarity.net
Fri Apr 8 03:48:52 UTC 2011


Hi Robert,

On Apr 7, 2011, at 2:25 PM, Robert Lusby wrote:
> Any ideas on the cleanest way to list every page (inc pages, posts, and
> custom post types) in a site?
> 
> I only require ID, Title and Permalink(URL) for each page. 
> 
> Any suggestions for the least intensive way to simply get these three items?

Copy the following code into the root of your WordPress website as something like 'tsv-export.php' and then load the file in your browser and you'll get a tab-separated list of what you need.

<?php
include('wp-load.php');
header('Content-type: text/tab-separated-values');
$query = new WP_Query(array(
	'post_type' => 'any',
	'post_status' => 'publish',
	'post_per_page' => -1,
));
foreach($query->posts as $post) {
	$title = get_the_title($post->ID);
	$permalink = get_permalink($post->ID);
	echo "{$post->ID}\t\"{$post->post_type}\"\t\"{$title}\"\t\"{$permalink}\"\n";
}

Even though it does filter out 'nav_menu_item' and similar posts, WP_Query does load everything including content, something you may not want.  If you don't want to load everything per record, here's another approach (you can add addition WHERE clause criteria to exclude items you want to exclude):

<?php
include('wp-load.php');
header('Content-type: text/tab-separated-values');
global $wpdb;
$posts = $wpdb->get_results("SELECT ID,post_type FROM {$wpdb->posts} WHERE post_status='publish'");
foreach($posts as $post) {
	$title = get_the_title($post->ID);
	$permalink = get_permalink($post->ID);
	echo "{$post->ID}\t\"{$post->post_type}\"\t\"{$title}\"\t\"{$permalink}\"\n";
}

Hope this helps.

-Mike



More information about the wp-hackers mailing list