I look up a custom post field value in header.php, and store it in a global variable.

In most of my template files, the includes go like this:

include header

...do a bunch of stuff

include sidebar
include footer

I can not persist the variable into the footer.php file...

is there any way to do this so that I can avoid having to call back to the database again to look up the custom field value again?

  • This is a PHP question, not a WordPress question. Feb 26, 2012 at 2:29

2 Answers 2


You'd have to declare it first above those includes and globalize it within header or footer before getting or setting the value.

however- in the specific context you're speaking of- getting a custom field value, it's only retrieved from the database on the first call, then cached, so subsequent calls won't hit the database again.


You can...

Create a function in functions.php file that asks for the custom field and then outputs it... then you need not include it in the header but can just use it wherever you want...

I assume you know how to retrieve the custom field value, but in any case here is an example from the wordpress codex:

echo get_post_meta( $post->ID, 'mykey', true );

I hope its a good solution if I understand you correct.

Cheers, Sagive.

  • Thanks for the response... this does not achieve the purpose. The problem is, even if I did as you suggested, I still need to make a call each time to the function, which in turn, makes a call to the db to get the value, right? I want to load the value into the variable the first time, and have it persist until the end of the footer.php
    – GWR
    Feb 26, 2012 at 0:28
  • that would mean (as far as i know) either creating a session or send it between pages using the url (which is complecated in wordpress)... in any case, a custom field value should be drown from the database... i dont know why people are fearfull to use the DB.. thats why its their :) - its a small query - no harm..
    – Sagive
    Feb 26, 2012 at 1:45

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.