Skip to content

Instantly share code, notes, and snippets.

Created July 26, 2012 08:41
What would you like to do?
Get WordPress permalink from post, page or custom post type name
* Gets the permalink from the post slug
* @param string $name the post, page or custom post type slug
* @param string $p_type the post type
* @return string The permalink YAHHH!!!
function get_perm_byname( $name, $p_type = 'page' ) {
global $wpdb;
$where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $p_type );
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_name = %s", $name );
$id = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE $where" );
return get_permalink( $id );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment