Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export list of pretty permalinks and post title

Looking for a way to export a list of pretty permalinks in WordPress with the corresponding post title. Looking for the actual permalink structure defined not the shortlink. I suppose if I have to, I will use a short link, but I prefer the full permalink.

like image 323
jeff Avatar asked Aug 12 '10 04:08

jeff


2 Answers

answered this one on EE this morning :) http://wp.daveheavyindustries.com/2011/02/08/wordpress-permalink-via-sql/

this query should do it for you

SELECT  wpp.post_title,
        wpp.guid,
        wpp.post_date,
        CONCAT
        (
          wpo_su.option_value,
          REPLACE
          (
            REPLACE
            (
              REPLACE
              (
                REPLACE
                (
                  wpo.option_value, 
                  '%year%',
                  date_format(wpp.post_date,'%Y')
                ),
                '%monthnum%',
                date_format(wpp.post_date, '%m')
              ),
              '%day%',
              date_format(wpp.post_date, '%d')
            ),
            '%postname%', 
            wpp.post_name
          )
        ) AS permalink
  FROM wp_posts wpp
  JOIN wp_options wpo
    ON wpo.option_name = 'permalink_structure'
   AND wpo.blog_id = 0
  JOIN wp_options wpo_su
    ON wpo_su.option_name = 'siteurl'
   AND wpo_su.blog_id = wpo.blog_id
 WHERE wpp.post_type = 'post'
   AND wpp.post_status = 'publish'
 ORDER BY wpp.post_date DESC 
like image 65
David George Avatar answered Nov 08 '22 23:11

David George


Here's a standalone PHP file you can save into the root of your website called something like /export.php and when you call it with your browser it will send a tab-delimited plain text list of posts with the pretty permalink, the post title and (as a bonus) the post type.

Just load the URL in your browser and then "save as" to a text file you can then load in Excel or however else you need to process it.

<?php

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;
    }
    echo "\n{$post->post_type}\t{$permalink}\t{$post->post_title}";
}

Hope this helps.

-Mike

P.S. I used the standard WordPress WP_Query() but also included a commented-out SQL in case you prefer (or need) to use it instead.

like image 44
MikeSchinkel Avatar answered Nov 08 '22 23:11

MikeSchinkel