WordPress.org

Ready to get started?Download WordPress

Forums

How to get Page Id using PHP (6 posts)

  1. mw247
    Member
    Posted 5 years ago #

    Anyone know how I can get the page ID using a php function?

  2. educationthemes
    Member
    Posted 5 years ago #

  3. Derek Herman
    Member
    Posted 5 years ago #

    This might not be what you're after but it has proven to be useful for me and some of the advanced things I do with WordPress.

    // Get the id of a page by its name
    function get_page_id($page_name){
    	global $wpdb;
    	$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
    	return $page_name;
    }

    If you had a custom query and needed to know the id of a page you could use get_page_id('portfolio') and it would return the id of portfolio.

  4. elC0mmen
    Member
    Posted 5 years ago #

    I had been looking for this for ages! Thanks valendesigns!

  5. funkboybilly
    Member
    Posted 5 years ago #

    You rock... perfectly what i needed.

  6. dthom
    Member
    Posted 4 years ago #

    Using built in stuff:

    $page=get_page_by_title($page_name);

    This provides the entire page record. $page->ID is the id

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags