Get a free trial // Grow your CSS skills // Land your dream job

Get ID from Page Name

Last updated on:

Add to functions.php file:

function 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."' AND post_type = 'page'");
   return $page_name_id;

Now you can use this function in templates when you need an ID of a specific post/page and all you have is the name.


  1. Cmet
    Permalink to comment#

    dont forget escaping for security, especially if you don’t have full control over the use and input of this function.

    mysql_real_escape for instance,

  2. Aldo
    Permalink to comment#

    There is already a function in the WordPress core to make this:

    get_page_by_title( $page_title, $output, $post_type );

  3. Silvana
    Permalink to comment#

    Sorry but get_page_by_title searches for title and not for name

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed