Get Permalink by Name

September 2nd, 2008

If you have a post or a page and need the permalink for it there are serveral solutions. Here are one of them. I created a name to permalink converter.

Put the following code to the functions.php in your theme folder.

function wt_get_permalink_by_name($page_name)
{
	global $post;
	global $wpdb;
	$pageid_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
	return get_permalink($pageid_name);
}

Call the function like this in your theme:

<a href="<?php echo wt_get_permalink_by_name('about'); ?>">About</a>

To get all new functions/template tags, install my plugin WP Extra Template Tags.

  1. 2011-06-17 - john

    This may have changed since you posted it, but it should be post_title to work.

  2. 2010-11-09 - thinkdj

    Exactly what I was lookign for. Thanks :)

  3. 2010-07-12 - Ramesh

    How to get Permalink of a page in-side the Plugin function?

  4. 2008-11-04 - Merrick

    Wow!! It’s getting better and better. Keep it up man.,