Example of the code:

$page_id = 116; // 123 should be replaced with a specific Page's id from your site, which you can find by mousing over the link to edit that Page on the Manage Pages admin page. The id will be embedded in the query string of the URL, e.g. page.php?action=edit&post=123.
$page_data = get_page( $page_id ); // You must pass in a variable to the get_page function. If you pass in a value (e.g. get_page ( 123 ); ), Wordpress will generate an error.

$content = apply_filters('the_content', $page_data->post_content); // Get Content and retain Wordpress filters such as paragraph tags. Origin from: http://wordpress.org/support/topic/get_pagepost-and-no-paragraphs-problem
$title = $page_data->post_title; // Get title
echo $title; // Output Content
echo $content; // Output Content

Instead of 166 (which was manually entered), I would like to retrieve the ID of the current page.

When i do $page_id = $post -> ID it retrieves the title and content of thte first post of the loop below (this is the posts page):

<div class="container">
                <?php // find all content that has the type of video and then to loop through them. ?>
                <?php query_posts('showpost'); ?>

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

                                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                                    <?php if ( is_front_page() ) { ?>
                                        <h2 class="entry-title"><?php the_title(); ?></h2>
                                    <?php } else { ?>
                                        <h1 class="entry-title"><?php the_title(); ?></h1>
                                    <?php } ?>

                                    <div class="entry-content">
                                        <?php the_content(); ?>

php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
                                            <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
                                        </div><!-- .entry-content -->
                                    </div><!-- #post-## -->

                                    <?php comments_template( '', true ); ?>

                    <?php endwhile; ?>

I would like to retrieve the code of the current page dynamically (which is 116).

What is the code to accomplish that?


From quick test $wp_query->get_queried_object_id() should get page's ID when that page is set to be posts page.

This is likely the issue of timing it late enough that it is available, but early enough so that loop of posts doesn't interfere. I'd try to capture it early (in template_redirect hook or around that) and store in global variable to use later.

global $wp_query;
$page_id = $wp_query->get_queried_object_id();

but if you're using a custom page for posts, where are you adding this code?

  • It is a Page used to show blog entries. – janoChen Jan 28 '11 at 14:14

There's two methods, depending on if you're doing this inside or outside the loop.

Inside: $page_id = $post->ID; (which you've mentioned, with no success, so I'll assume you're try for the alternative which is...)

Outside: $page_id = $wp_query->post->ID;

  • it doesn't matter what I do, it still retrieving the posts of the loop below that function. – janoChen Jan 26 '11 at 10:57
  • If you are using that second block of code in the same page as your get_page function, then its most likely to be the query_posts function that is upsetting everything. You will either need to create a custom query or reset the loop. Try commenting out second block from the template ( prevent the php from executing) to see if that works. – Mike Hudson Jan 26 '11 at 11:06
  • it doesn't matter if I delete the second loop. The page is set to be the posts page, so I think the loop is being applied "globally or something". – janoChen Jan 26 '11 at 11:15

I replaced:

$page_id = [id of post];


$page_id = $wp_query->get_queried_object_id();

Worked for me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.