Skip to content

Instantly share code, notes, and snippets.

@c3mdigital
Created July 26, 2012 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c3mdigital/3181025 to your computer and use it in GitHub Desktop.
Save c3mdigital/3181025 to your computer and use it in GitHub Desktop.
Get WordPress permalink from post, page or custom post type name
<?php
/**
* 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