Dec 19, 2008 - PHP, Wordpress    45 Comments

WordPress: get page id and content with example & code

Hey Guys!

Often I see people coming to my site searching with the terms wordpress get page id, wordpress get page content or something similar. I think wordpress developers face situations when they need to get page or post information in customized way. It happened to me too and I would like to share it with others since often people are coming to search this information.

Well. It is very easy and we can solve it quickly.
OK. Now let me show you how to get these information.

Remember one thing that is important for wordpress data fetching of this kind. Your posts and pages information is saved in a single database table and that is wp_posts. The wp_ is the prefix of you database and may differ. But most of the times the database table name is wp_posts and other times it is YourCustomPrefix_posts. We are going to fetch data from this table.

WordPress – print page id:

There is a built-in wordpress function using which you can print the post or page id. When you call this, this directly prints this inforation in your page without the need to use the built-in PHP echo or print function. Remember to keep it in the while loop.

WordPress – get page id:

There is a global variable post which contains the related information of the currest post or page. The name of the variable is: $post and it is actually an object. You can access information just as you access variables from an object. Remember to keep it in the while loop.

You can print all the information in the $post object to see all the variables and their values that is contains.

WordPress – get page content:

Now you know you can get the page or post content from $post->post_content variable. But if you echo or print them, they may look somewhat without formatting. So, you need to use PHP built-in nl2br() function to look the content as it is.

WordPress – get any information of your page or post:

You know you can print all information to check all the available variables and their values through using PHP’s built-in var_dump() function. Suppose you need to get the post title, post type and posting time. We can get them easily in this way.

Please notice that post_title, post_type and post_date are all several database fields from wp_posts table of our wordpress database.

Very easy. Right? And that is all for getting the page or post information.

Thank you for reading.


  • Great post. Thanks for sharing!I also have an online printing website to recommend. Check out at my website.118

  • Thanks for the info. I have been looking for how to add page names as ids to the body of the design so I could implement a CSS sprite navigation. This was superhelpful.

  • Great!

    Can you update this post, how can I get the meta tags informations?


  • @ Tóth

    if you do some googling, you will find meta tag setting plugins.


  • I’m trying to use the Included Page plugin which pulls in data from another page by using PHP, (written as iinclude_page(pageid);) but for that data to be different depending on what page it is on.
    Now, what I think I should do is have the code do something like this, but I dont know what the variables that identify what the page we’re looking at would be:

    if ((thispageis == ‘pageidentifier’) {
    ?> iinclude_page(4); iinclude_page(5);

    Could you help me figure out what the variables would be?

  • The reason for this is that the sidebar needs to change depending on what page a person is on, and those sidebars also need to remain editable through the WP editor in the admin area – the main backend user knows no code.

  • I tried the “WordPress – get page content:” function but when I loaded the page it gave me an error. “Parse error: syntax error, unexpected T_ENDIF in ”
    This function was included on a seperate page from the main index.php page.
    Could this be the problem I am having with this function? I’ve been trying to find out how to call other posts that have been written into my Recipes.php page. I’ve looked all over but with no luck. Any help you can give would be very appreciated! Thanks.

  • @ Sparkyjoe

    delete the single quotes(‘) and double quotes(“)
    and manually type them.

    Often copying and pasting the code in editor does not work because of single and double quotes problems.

    Once again delete them and type them in proper way.
    This should solve the problem.

  • I figured out my problem, well one problem. I founf that I had an extra
    . I deleted it and that atleast displayed the page that i was wanting but I was thinking it would display the titles and content to all the pages I was calling.
    Is there any way to make multiple pages and have them displayed like posts on a seperate page that I specify without displaying them on my main index.php page?

  • Sorry my last post didn’t display the tag I mentioned. It was an extra “endif” tag that I had in my code.

  • @ Sparkyjoe

    It is possible.
    It depends how you do it.

    You can write a function and pass post ids.
    Thus, you can get multiple posts’ information.

  • ok, last try:

    i’ve got a homepage/index.html.
    In that the content of wordpress/?page_id=11 shall be displayed.

    Where do i have to put the function? in the template of wordpress or in the index.html?

    And how to call the content in the html?

  • @ pcuserman

    In an HTML file, you can never put dynamic content
    first of all, you must make it a .php file.

    download the default wordpress theme and examine the files.
    so, you will get a better answer to your question

    also you can take a look at my other posts for a solid idea for your question’s answer:

  • too hard to give a quick solution, huh?

    sorry, but instead of telling me what to do you easily could have told me in 3 steps where to put these functions in^^

  • @ pcuserman

    keep your functions in functions.php file in your theme folder and you can access it from anywhere

  • thank you that’s it 😀

  • Thanks mate, “$post->ID;” is actually what I was looking for.



  • Really a fantastic post which saved my time. Thanks a lot. 🙂


  • Thank you for the great, post it has been much appreciated and implemented into our upcoming website due to be released by Thursday 19th November 2009.
    It’s a nice work around for the non-working ‘get_page();’ function.

  • what if a page is used to house the posts. will the page id of that page change? this usually the case when using a static page for homepage. doing so won’t let you call that page thru is_page() using its id.

  • @ potobenka

    there is a function wp_list_pages

    also there are many other ways such as custom coding to get the posts!

  • Hi, can you help me please ?
    example :
    I have wordpress page with name :

    I want the content of that page can appear in my home page.
    Only content from that page (Not Tittle or etc).

    so, what the php code must put in my div table ?
    I try using your code above, but all contents of the posts appear in my div table at home page.

  • sorry, I am not familiar with php code.
    I has seen your link reference, but I still confuse.
    can you please give me the code for make appear the page above like I told ? So, I can put the code in my table.
    Thank you.

  • Thank you… I understand now..

  • How to get page ID outside the loop ?
    I’ve try using global for $post, but didn’t work.

  • Thanks that rules! I knew I’d learned this before….stumbled around google then came (back) to your site. I knew this is where i’d learned it b’c you have a beautiful site.


  • Hi, thanks for this interesting post!
    I have one more question: Is ther a way to display in a sitemap the page title + page ID?

    I’m using this line to display all pages + subpages:

    And I’d like to have the output like
    “Home – 1”
    “Page XYZ – 2”

    Is there a way to do this?

  • @ Mandy

    You can write a custom function in the functions.php file where you can show the pages and ids something like:

    < ?php function getSitemap() { global $wpdb; $output = ''; $sqlQuery = "SELECT * FROM {$wpdb->prefix}posts
    WHERE post_status = ‘publish’
    AND post_type=’page’
    ORDER BY post_date DESC; “;
    $dbResult = mysql_query($sqlQuery);
    while($dbRow = mysql_fetch_object($dbResult))
    $output .= $dbRow->post_title . ‘ – ‘ . $dbRow->ID . ‘
    return $output;


    Then you call it in your sitemap Template file:
    < ?php getSitemap(); ?>

    Give a try!

  • Great post!
    I had this problem, where I wanted to show something on all pages but the first page. Very easy after I read your post and know how to get the ID of the first page.

    Thank you SO much!

  • This post just saved my life! Thank you!
    Why it’s so hard to get the page id or page title in WP, I don’t know…

  • thinks are clear…

  • Hello!
    This is great! Thanx!

    I have one question, maybe you can help me. I keep trying things but still no success!

    I got four boxes on my home page, and in each one, my client wants now to display content from 4 static pages. I am using the following code to do that:

    $title = $page_data->post_title;
    echo $page_data->post_title;
    echo $page_data->post_excerpt;

    They want also to have the featured image to display in the box, together with the title and the excerpt. How can I achieve that?

    I can’t have the content to display, because they want the image to stay a fix size, and the page content might have more than one image, but only the featured one should show up in the home page.

    Thank you!


  • @ Chiara

    you can get and show the content of as many pages as you want in any page.
    Just use “query_posts” function

    For more, check:

  • Great post, very helpful. Thanks.

  • Thanks for sharing your post! Really helpful!

  • Great Post
    This is really very helpfull to us

    Thanks for sharing… 🙂

  • Woow . . . .this post help me very much ! thanks broo

  • Thanks! This was of great help :>

  • Thanks for sharing the great tutorial.. It’s very helpful.

  • Thanks you 🙂
    Very helpful for my duties

  • your function is not great as it cutting off the paragraph tags of the content putting instead . nl2br is what is causing it.


    post_content); echo $content; ?>


  • Worked like a champ. Thanks so much for the help!

  • please help me i trying to get different post content according to there ID but i am unable to get either title or content in the

    Please how can i fix this help me. If its possible to reply me on my email then that would be much better [email protected]

    9, ‘category’ => 7 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post); ?>
    ID ), ‘single-post-thumbnail’ ); ?>
    ID); ?>

    <!–<img alt="Portfolio Item" src="/images/portfolio_images/9.png” class=”darkBorderFrame” />–>

    <a rel="group_fancybox" title="”
    href=”” class=”icon_expose”>

  • Thank you!

Got anything to say? Go ahead and leave a comment!