WordPress.org

Support

Support » How-To and Troubleshooting » Get page ID by title via function

Get page ID by title via function

  • Hi!

    Is there a function that returns a page’s ID by its title or ‘nicename’? (Similar to how is_page works…)

    I want to exclude a page from the wp_list_pages list, but don’t know the page’s ID, only its nicename…
    And unfortunately, wp_list_pages only excludes by ID… 🙁

    That’s why I’m looking to get a page’s title by its ID outside the loop.

    Kind regards,
    Christian

Viewing 6 replies - 1 through 6 (of 6 total)
  • You can get the ID from Manage > Posts — it’s in the first column on the left (ID is a number).

    Yes, well, I was actually thinking of a function that I could use inside a template file…

    the_ID() works within the Loop, but echoes the ID so you can’t use it in PHP.

    You are only doing this once, for a specific title? Look at the ID from the manage page, then use that ID in function calls in code. Done.

    -d

    You can select it from the database if you need to do it automatically.

    This is most probably a horrible kludge, so I’d love to hear of a better method if anyone knows of it:

    global $wpdb; // you may not need this line
    $post_name = get_query_var('name');
    $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE
    post_name = $post_name");

    I think that’s the right query_var there, but it might not be name.

    That should only be used when global $id doesn’t work. You might be able to declare global $id in a template file, and then use $id whenever you need to ID.

    Also, this might work:

    global $post; // you might not need this
    $post->ID;

    That would be much less resource-greedy, if you can get this to work, go for it.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get page ID by title via function’ is closed to new replies.