I'm trying to use a predefined variable (generated from an earlier sweep of the database) in wordpress' standard get_permalink( get_page_by_title) function.

I'm assuming that this is possible? But I can't get it working, it always returns the url of the current page.

For the sake of clarity below I'm just manually assigning the variable.


<?php $mypagename = "My First Page"; ?>
<a href="<?php echo esc_url( get_permalink( get_page_by_title( $mypagename ) ) ); ?>">LINK</a>

N.B - My First Page (my-first-page) does exist in the database

Any ideas where I'm going wrong or if it's not possible to do it like this do you have any other suggestions?

Many thanks!

  • Try var_dump( get_page_by_title( $mypagename ) ). I bet you get NULL. That's why get_permalink is returning the URL for the current page. Are you sure the "My First Page" page exists? Jan 20, 2015 at 17:02

1 Answer 1


get_page_by_title() returns a object by default and get_permalink() needs the ID as first parameter. Try:

$mypagename = "My First Page";

$permalink = get_permalink( get_page_by_title( $mypagename )->ID );
  • Ahh that will be why it's not working then ;) Thanks!
    – Grant
    Jan 20, 2015 at 17:29

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.