[wp-hackers] Access $post near template_include

Mike Schinkel mike at newclarity.net
Sun Jul 22 19:22:35 UTC 2012

On Jul 22, 2012, at 8:21 AM, Mario Peshev wrote:
> I'm trying to do some on-the-fly manipulations on the page template that
> take place in template-loader.php, by filtering the template_include part
> at the end. However I am not sure how to get the post ID there as I still
> don't have an access to the global variables for post and the loop and I
> also can't get it from the server variable due to permalink specifics.
> Is there a workaround to get the post ID at that point of loading?

The global variable $post is available by the time the 'template_include' hook fires.  Add this to your theme's functions.php file to see it in action:

add_filter( 'template_include', 'marios_template_include' );
function marios_template_include( $template_file ) {
  global $post;
  echo "Post ID: " . $post->ID . "</br>";
  return $template_file;

Hope this helps.


More information about the wp-hackers mailing list