1

I asked a very similar question earlier today (http://wordpress.stackexchange.com/questions/30392/how-to-get-the-original-post-id-of-a-static-home-page) and thought I had the answer, but now it appears otherwise.

If I have a post/page with the original permalink of http://mydomain.com/blog/my-post/ for example, and I set that as the static home page of the blog, I am unable to get the original permalink of that page when I visit it, since WP only returns the root (site_url() essentially) permalink and redirects me there, since it is now set as the home page.

How can I retrieve the ORIGINAL permalink? get_option('page_on_front') doesn't work because it will just return the blog root url, such as http://mydomain.com/blog/. I need to fetch the full original unaltered permalink of the currently-set static home page.

Hope that's clear... thanks all!

1

Edit: Wow. I mistyped a bunch the first time around. Hopefully nobody read that. All corrected now. /edit

I went code spelunking... :)

Short version, the workaround would be this:

<?php 
    $front_page_id = get_option( 'page_on_front' );
    $my_permalink = _get_page_link( $front_page_id ); 
?>

If you look into link-template.php in the WordPress core, you can kind of trace the path that happens. Calling the_permalink() leads to get_page_link(), which returns the permalink of the post/page ID you pass it. However, one of the checks that get_page_link() does is to see if the requested ID is currently set as 'page_on_front'. If it is, a static value of '/' is returned as the permalink. If it's not, the function _get_page_link() is called instead. Note the underscore on the front of the function.

In order to solve the issue, I cut past the_permalink() and get_page_link() and went straight to _get_page_link().

There may be some security issue or reason for this behavior to be default that I'm not aware of, but it works...

| improve this answer | |
  • Holy cow... way to go hunting! Let me see if this is the answer it appears to be for me and I'll report back shortly! – JVC Oct 7 '11 at 0:49
  • 1
    SO SWEET... This is exactly what I was after. Nice of it to be so deeply buried! ;) Thanks for the help, I'd upvote you if I had enough rep here! – JVC Oct 7 '11 at 1:15
  • No problem. This is a great way to learn new stuff. :) – Jeremy Felt Oct 7 '11 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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