Get ID by Page Name

September 2nd, 2008

Functions like wp_list_pages() can be really useful in WordPress but there are things that I don’t like.

For include or exclude pages you need the ID. For me it feels better to use the page name instead, simply because the ID can change but the name does not. I have written a function fix this issue.

The function converts a page name to a page ID. You can use it however you like but in the example I used it with the wp_list_pages() function.

function wt_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;

Call the function in your theme:

<?php wp_list_pages('include='.wt_get_ID_by_page_name('about') ); ?>

If you just need to display the ID of a page, call the function like this instead:

<?php echo wt_get_ID_by_page_name('about'); ?>

To get all new functions/template tags, install my plugin WP Extra Template Tags.

  1. 2011-12-09 - Mahabub

    thanks for this code

  2. 2011-06-13 - John

    Why don’t you use and then use the object ?
    I don’t think performance would be highly impacted here and calling the db directly can lead to unexpected behavior especially if filters are applied (I’m thinking of wpml’s autoid feature).

    Just my 2 cents.


  3. 2011-03-21 - Ryan

    I love this function, but ran across and instance where it didn’t work. In the database there was an autosave of a post and this function reported the ID of the autosave post and not the published post. The only way I could fix it was to delete the autosave post from the database and then it worked correctly. Is there any way to not have to do that?

  4. 2010-12-24 - Sisir

    I will take the idea to use it with the post type ‘page’ just to be safe. And i also call the function get_id_by_page_slug() :) as it works for slugs instead of page name. Suppose if you have a page name contains two character it wont work until you give the “-”.

    Thanks for the idea ;)

  5. 2010-09-22 - kevin

    @Bill, just tested with accents and doesnt work. The way around i found was to create a new page named “Luiz”, use this name in the php and then modify the page name for “Luiz Varejão”. WordPress keeps the 1st name you give to the page. However spaces seem to work as wp tranlates them into “-” like for posts. So i tested to call my page “Quem somos” with “Quem-somos” and works. However i cant find the page slugs in the ‘edit page’ admin page.

    Anyway, very usefull function!

  6. 2010-07-29 - rob

    thanks! nice

  7. 2010-04-19 - Dwight

    Thanks. It helps a lot. If you truly want only PAGE results and no POST results mixed in and you have pages and posts which may have the same title, use this:

    $page_name_id = $wpdb->get_var(“SELECT ID FROM $wpdb->posts WHERE post_name = ‘”.$page_name.”‘ AND post_type = ‘page’ “);

  8. 2010-04-04 - Hikari

    WordPress should have an API function for that :/

  9. 2010-02-23 - Bensbury

    Hi, I found this example here in the comments:

    It seems to do the job for getting page names.
    Can’t believe WP doesn’t have this in the core.
    Especially when some WP peeps start saying how it has caught up Drupal.

    Hope that saves some other people the pain for having to id pages.

  10. 2010-02-22 - Ryan

    I was looking for a good 2 hours for a way to do this. Thanks so much!

  11. 2010-02-11 - Lars

    Tnx, I’ve been using your hack to get this functionality. I notice that the SQL works better for me if I use “… WHERE post_title=’”.$page_name.”‘”);

  12. 2010-01-08 - Andy

    Nice! Exactly what I needed; seems like this should be in the core.

  13. 2009-12-11 - Bill

    I’ve been messing round with this for ages. This code works fine if your title contains straight alphanumeric characters. But if your title contains an apostrophe, or an “&” symbol, this code will not work unless using the page slug instead.

    Anyone know of a workaround for this?


  14. 2009-06-20 - Jesper

    This is a really nice function, but is it possible to list only the pages you want to include, only 3 of 10 pages of a WordPress powered site?

    Also, is it possible to include title_li= and if so, how?

    I would really appreciate this ;)

  15. 2009-06-17 - Evan

    I’ve been working on this problem all week! Thank you so much!!!

  16. 2008-10-17 - ant

    can it used to someting like

    thanks for your help

  1. vtxyzzy on "Get page content using page name" | Upgrade Wordpress Now | Ignacio Gondra | Design | For little and middle-sized enterprises , Web Design, Web Development, Information Architecture, Project Consultancy