Learning Is Fun

Talks on Web Technology and Better Product Development

WordPress: get page id and content with example & code

December19

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.

<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
		<?php the_ID(); ?>
	<?php endwhile; ?>
<?php endif; ?>

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.

<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
        <?php
		global $post;
		echo $post->ID;
        ?>
	<?php endwhile; ?>
<?php endif; ?>

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

<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
        <?php
		global $post;
		var_dump($post);
        ?>
	<?php endwhile; ?>
<?php endif; ?>

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.

<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
        <?php
		global $post;
		echo nl2br($post->post_content);
        ?>
	<?php endwhile; ?>
<?php endif; ?>

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.

<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
        <?php
		global $post;
		echo $post->post_title;
		echo $post->post_type;
		echo $post->post_date;
        ?>
	<?php endwhile; ?>
<?php endif; ?>

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.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
posted under PHP, Wordpress
31 Comments to

“WordPress: get page id and content with example & code”

  1. On December 27th, 2008 at 11:49 am Great Printing Site Says:

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

  2. On January 2nd, 2009 at 4:39 pm curtismchale Says:

    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.

  3. On January 12th, 2009 at 4:57 am Tóth András Zoltán Says:

    Great!

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

    ;)

  4. On January 12th, 2009 at 5:03 am admin Says:

    @ Tóth

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

    thanks.

  5. On February 6th, 2009 at 7:19 pm pz Says:

    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?
    Thanks!

  6. On February 6th, 2009 at 8:23 pm pz Says:

    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.

  7. On February 11th, 2009 at 11:05 am Sparkyjoe Says:

    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.

  8. On February 11th, 2009 at 11:12 am admin Says:

    @ 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.

  9. On February 11th, 2009 at 12:54 pm Sparkyjoe Says:

    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?

  10. On February 11th, 2009 at 12:57 pm Sparkyjoe Says:

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

  11. On February 11th, 2009 at 2:46 pm admin Says:

    @ Sparkyjoe

    Yes.
    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.

  12. On March 24th, 2009 at 3:20 pm pcuserman Says:

    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?

  13. On March 24th, 2009 at 3:33 pm admin Says:

    @ 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:
    http://www.tanzilo.com/category/wordpress/

  14. On March 24th, 2009 at 3:57 pm pcuserman Says:

    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^^

  15. On March 24th, 2009 at 3:59 pm admin Says:

    @ pcuserman

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

  16. On March 24th, 2009 at 4:00 pm pcuserman Says:

    thank you that’s it :D

  17. On May 13th, 2009 at 11:55 am Benoit Tremblay Says:

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

    Cheers,

    Ben.

  18. On May 28th, 2009 at 3:29 am Sankar Says:

    Really a fantastic post which saved my time. Thanks a lot. :-)

    San

  19. On June 7th, 2009 at 10:43 am language translation software Says:

    You have a great blog here and it is Nice to read some well written posts that have some relevancy…keep up the good work ;)

  20. On November 16th, 2009 at 9:10 am Kyle Welsby Says:

    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.

  21. On January 11th, 2010 at 2:41 am potobenka Says:

    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.

  22. On January 11th, 2010 at 5:14 am admin Says:

    @ potobenka

    there is a function wp_list_pages

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

  23. On January 12th, 2010 at 5:01 am Yudi Says:

    Hi, can you help me please ?
    example :
    I have wordpress page with name :
    http://127.0.0.01/mywordpress/?page_id=67

    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.

  24. On January 12th, 2010 at 6:05 am admin Says:

    @ Yudi

    Check this:
    http://codex.wordpress.org/Template_Tags/query_posts

  25. On January 12th, 2010 at 7:15 am Yudi Says:

    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.

  26. On January 21st, 2010 at 2:49 am ebta Says:

    Thank you… I understand now..

  27. On January 21st, 2010 at 2:50 am Bisnis UKM Says:

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

  28. On January 21st, 2010 at 6:21 pm Jason kadlec Says:

    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.

    Thanks!

  29. On February 6th, 2010 at 9:37 am Mandy Says:

    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″
    ect.

    Is there a way to do this?
    Thanks!

  30. On February 6th, 2010 at 1:04 pm admin Says:

    @ 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!

  31. On March 18th, 2010 at 2:07 pm Ateljedigital Says:

    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!

Email will not be published

Website example

Your Comment: