WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a <a href="Something Like <?get_contact_page>">Contact Page</a> Code that always links to the Contact page, no matter what is it's ID

There's a page template for contact page.

Is it possible to do this?

I'll gladly provide any further information you need :)


share|improve this question
Lucas, please see: How does accepting an answer works? – brasofilo Feb 21 '13 at 16:09
I'm sorry, I've posted the question and slept!! I never fail to accept an answer and thank everyone who helped :) – Lucas B Feb 21 '13 at 19:17
up vote 1 down vote accepted

If the contact page is identified solely by the associated template, you can query for a page with the template name in meta key _wp_page_template:

$args = array(
    'post_type' => 'page',
    'posts_per_page' => 1,
    'meta_query' => array(
            'key' => '_wp_page_template',
            'value' => 'contact_template.php'
$contact_page = new WP_Query( $args );

if( ! empty( $contact_page->posts ) )
    echo get_permalink( $contact_page->post->ID );
share|improve this answer
Damn, you beat me! I pretty much provided the same code, except I created a function and account for instances where a template is assigned to more than one page and support returning an array or echoing single keyed values in the return array. – Dwayne Charrington Feb 21 '13 at 1:48
Thank you for your help!! I'll do it right away! – Lucas B Feb 21 '13 at 19:25

You're in luck. Wordpress stores a pages template value in a hidden custom field not visible in the backend but very much exists called, "_wp_page_template". I created the following function which should return an array or a single key of data for you.

I put the code on Github Gist here because it's a little long. I did some testing and it seems to work! Should do what you want it too.

share|improve this answer
Thanks for sharing your knowledge, question answered! – Lucas B Feb 21 '13 at 19:26

There is a function called get_page_by_title() that you might find useful:


If the title is always "Contact Page" then you can use

<?php $page = get_page_by_title("Contact Page");?>

<a href="<?php echo get_permalink( $page->ID ); ?>">Link to my Contact Page</a>
share|improve this answer
This won't work in the instance of a client website and they decide that the page which was called, "Contact Us" when the site was built would read better if it said, "Get in Contact" which would break this code. Good idea in theory, but you should never assume things will be constant (especially page titles). – Dwayne Charrington Feb 21 '13 at 1:44
I agree, I somehow missed the "template" sentence of his question ;-) – birgire Feb 21 '13 at 1:48
Happens to the best of us man! :) – Dwayne Charrington Feb 21 '13 at 1:50
your get_page_by_template() function looks very useful, nice. – birgire Feb 21 '13 at 1:57
Thanks man. It's probably a little rough around the edges, but seems to get the job done! I might go and clean it up a little later because it's a pretty useful function. – Dwayne Charrington Feb 21 '13 at 2:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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