Get the page ID by the page name

Add the first snippet into the functions.php and the second snippet into one of your wordpress template files. This snippet will allow you to return a page ID by the page name. Don’t forget to change PAGE_NAME to the name of the page you would like to return the ID of.

 

 

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

 

<?
$the_page_id = wds_get_ID_by_page_name('PAGE_NAME');
echo $the_page_id;
?>

Be Sociable, Share!

Leave a comment