Support » Fixing WordPress » How to get Page Id using PHP

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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.

    elC0mmen

    (@elc0mmen)

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

    You rock… perfectly what i needed.

    Using built in stuff:

    $page=get_page_by_title($page_name);

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to get Page Id using PHP’ is closed to new replies.