I am trying to create a function for my functions.php file that will allow me to convert a post name into a post id. I have had a look around online and managed to find this link http://www.devdevote.com/cms/wordpress-hacks/get_id_by_post_name which gives the following example..
function get_id_by_post_name($post_name)
{
global $wpdb;
$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$post_name."'");
return $id;
}
<?php echo get_id_by_post_name('my-post-name'); ?>
After replacing my-post-name in the example I am not getting any results, can anyone suggest what is wrong?
I was also looking into the get_by_postname function in case this was a better way of doing things.