WordPress Permalink Without Using GUID

Sometimes we have a need of pretty link instead of default link ….

Default link is http://mavendeveloper.com/?p=3

pretty link is http://mavendeveloper.com/2010/08/sub-domain-not-working-in-wordpress-3-0-or-wpmu/

if you are in loop the below code works well:

get_permalink($post->ID);

but sometimes getting pretty link is impossible when you have to construct pretty link outside of loop in custom requirement… in such a situation what can be done is … query the database and generate you own pretty link like shown below :

SELECT CONCAT(DATE_FORMAT(post_date,’%Y/%m/%d/’),post_name) AS permalink FROM wp_posts WHERE post_status=’publish’;

I will show you the complete example how to use this from which you will have clear idea :

example:

$sql_query = "select *,CONCAT(DATE_FORMAT(post_date,'%Y/%m/%d/'),post_name) AS permalink from ".$blogPostsTable." where post_status = 'publish' and post_type = 'post'
order by post_date desc limit 0,5";
$sql = mysql_query($sql_query);

while($array = mysql_fetch_assoc($sql)){

$recent_post3 .= ” “;

$recent_post3 .= ‘ ‘.substr($array[‘post_title’],0,45). ‘..‘;

$recent_post3 .= ” “;

}
echo $recent_post3;

Cheers

Leave a Reply