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.
-
Helps when we know the table(s) and columns involved - we don't all know the Wordpress data model– OMG PoniesAug 12, 2010 at 4:12
-
1@OMG Ponies: If you don't know the WordPress data model you wouldn't be much help answering this question; the permalinks are not stored in the database directly. Thankfully we'll all have a new WordPress Answers from StackExchange go public within the week! When that happens it'll probably make sense to route people asking WordPress questions there as we'll have plenty of users on hand who know the WordPress data model intimately as well as all other aspects of WordPress!– MikeSchinkelAug 14, 2010 at 2:50
-
@MikeSchinkel: Riight... because I've never had to learn someone else's data model before & provide query optimization feedback. If the questions were that prevalent, I'd have learnt the data model already. But good luck with that!– OMG PoniesAug 14, 2010 at 3:01
-
1@OMG Ponies: It's not learning the data model, is that to work with WordPress well you have to view it holistically integrating both database and framework knowledge. Expecting someone who doesn't know WordPress to give a good answer about how to best query WordPress just because they are a MySQL expert is like me expecting I could give a good answer on an Oracle PL/SQL question just because I have expertise in MySQL and MS-SQL. Knowing the platform matters.– MikeSchinkelAug 14, 2010 at 7:40
-
@OMG Ponies: One more thing, most people asking these kind of questions about WordPress won't know the data model; in part that's why they are using WordPress as it generally keeps them from having to learn it. (No slight to you @jeff as it seems by your tagging of the question that you are aware of MySQl; I was speaking in generalities.)– MikeSchinkelAug 14, 2010 at 7:44
3 Answers
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.
-
Thanks MIke for the help and understanding. I am getting a Warning: Invalid argument supplied for foreach() on line 7 for some reason– jeffAug 14, 2010 at 13:41
-
Sorry, I made an unfortunate edit just before posting the code, doh! Fixed! Aug 15, 2010 at 18:51
-
Thanks Mike, Old post but still very effective snippet that just did the trick ;-) Great!– quokkaJun 21, 2013 at 19:29
-
Hi, this code is not working on the latest Wordrpress version. Please kindly fix. Thank you Nov 8, 2013 at 11:38
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
I also wanted this solution and thanks @MikeSchinkle for the original solution. I did use this to export those links in plain text to excel and then build my redirect list.
But then I found that I also wanted a solution with live, active links.
So I used the wp_query
using the post type "any" and created a page template with a search form included with the following query (customize to fit your theme as you see fit). Note I had to set the posts_per_page
at -1 to return unlimited results. This returns results as: "Title - Permalink"
<?php
$type = 'any';
$args = array (
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'DESC',
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<?php the_title(); ?> - <a href="<?php the_permalink() ?>" rel="bookmark" title="View The <?php the_title_attribute(); ?>"><?php the_permalink() ?></a><br />
<?php endwhile; ?>
<?php else :
echo '<h2>Sorry, we didnt find any results to match. Please search again below or call us at 800-828-4228 and we will be happy to help!</h2>';
get_search_form();
endif;
$wp_query = null;
$wp_query = $temp; // Reset
?>
Hope that helps others.