Be Studios

Intercontinental Design Firm

This is our knowledge center. Enjoy!

Get ID by Page Slug

To get the ID by page slug in WordPress, insert this function in your functions.php:

<?php
function get_ID_by_page_slug($page_slug){
     global $wpdb;
     $page_slug_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name ='".$page_slug."'");
     return $page_slug_id;
} 
?>

And in the theme use it as:

<?php
$pageID = get_ID_by_page_slug('page-slug');
echo get_permalink($pageID);
?>