[wp-hackers] Getting every page ID, URL, and Title on site
Philip Walton
philip at philipwalton.com
Fri Apr 8 05:05:00 UTC 2011
Mike, your second approach still loads the content. It's the exact same
problem I had when writing my PW_Archives plugin. The function
get_permalink() accepts an object or an integer. If it's passed an
integer, WordPress runs a query for that post so it can create an object
(downloading the content from the database mind you), and then uses that
post object to figure out the permalink.
If you run an SQL log of your second method, you'll notice at least one
query per post, which is incredibly wasteful, but unfortunately what
most archiving/sitemap plugins do.
Philip
On 4/7/11 8:48 PM, Mike Schinkel wrote:
> 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
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
More information about the wp-hackers
mailing list