WordPress.org

Ready to get started?Download WordPress

Forums

Get page ID by title via function (7 posts)

  1. TigerDE2
    Member
    Posted 7 years ago #

    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

  2. maerk
    Member
    Posted 7 years ago #

    You can get the ID from Manage > Posts -- it's in the first column on the left (ID is a number).

  3. TigerDE2
    Member
    Posted 7 years ago #

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

  4. maerk
    Member
    Posted 7 years ago #

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

  5. davidchait
    Member
    Posted 7 years ago #

    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

  6. maerk
    Member
    Posted 7 years ago #

    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.

  7. maerk
    Member
    Posted 7 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags