Created
July 26, 2012 08:41
-
-
Save c3mdigital/3181025 to your computer and use it in GitHub Desktop.
Get WordPress permalink from post, page or custom post type name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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