Dave Heavy Industries Dave Heavy Industries – blog

8Feb/112

WordPress permalink via SQL

Today someone asked how to get the wordpress permalink from another language via SQL. Well, after a little bit of head scratching I have come up with a relatively easy way of doing this... enjoy!

SELECT
wpp.post_title,
wpp.guid,
wpp.post_date,
CONCAT(wpo_su.option_value,
REPLACE(
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 )
,'%category%',wpc.slug )
) as permalink
FROM
wp_posts wpp
INNER JOIN wp_options wpo on wpo.option_name='permalink_structure' and wpo.blog_id=0
INNER JOIN wp_options wpo_su on wpo_su.option_name='siteurl' and wpo_su.blog_id=wpo.blog_id
INNER JOIN (
select wtr.object_id ID, max(wpt.slug) slug
from wp_term_relationships wtr
inner join wp_term_taxonomy wtt on wtt.term_taxonomy_id=wtr.term_taxonomy_id and wtt.taxonomy='category'
inner join wp_terms wpt on wpt.term_id=wtt.term_id
group by  wtr.object_id
) wpc on wpc.ID=wpp.ID
WHERE wpp.post_type = 'post' AND wpp.post_status = 'publish'
ORDER BY
wpp.post_date DESC

update 9/2 - appended %category% join and replace

[facebook_ilike]

Comments (2) Trackbacks (0)
  1. How to append category to the above query..?

  2. How add post type to query and permalink.

    i.e: http://domain.ext/listings/my-title-here

    Custom post type: listings

    Thanks


Leave a comment

 

No trackbacks yet.

Switch to our mobile site