I'm getting a post by id with get_post();

$post = get_post(17);
$page = get_post_meta(17);
setup_postdata( $post ); 

After getting the post I include a pagetemplate wich contains a:

while ( have_posts() ) : the_post();    

If I call the_title() from outside the loop I get the right title of the post called with get_post(). If I call the_title() from within the loop I get the title of the current page I'm on. How do I get the data from the get_post into the loop.

  • 1
    Why are you loading a page template inside another page template?
    – Tom J Nowell
    Commented Aug 1, 2015 at 16:34
  • I'm creating a one page website, and want to give the admin the possibility to give the parts of the page an different layout. Commented Aug 1, 2015 at 17:19

2 Answers 2


If you are using setup_postdata( $post ) you need to use the global $post as for some reason it does not work without that specific variable. Once you manually set $post to a new value, you are altering the $post global, so you will need to reset it back to the current post of the main query.

Apart from that, you should never use any global variables set by Wordpress as a custom variable as this can cause non debuggable issues and unexpected results from your code. Breaking a global variable and then trying to debug such an issue will drive you up a mountain ;-)

In your specific use case, you need to add wp_reset_postdata() immediately after your custom query is done

$post = get_post(17);
$page_template = get_post_meta(17);
setup_postdata( $post ); 

There are multiple ways to handle these things and your question is a little light on detail why you need.

Specific to the title part — to retrieve title of specific post you shouldn't be messing with globals at all, just do $title = get_the_title( 17 ); or around that.

In most of the cases template tags have a version that retrieves data for specific post, not just current post in the loop. There is also a lower level get_post_field() to retrieve raw or nearly raw data for the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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