Let’s say you have some WordPress code that’s looping through all your posts and outputting some of their data to the webpage. How would you get the permalink of the page outside of The Loop (as in, what’s in the browser’s address bar) rather than the post you’re currently looping through?

Typically you can use WordPress’ get_permalink() function, but if you’re inside of The Loop, that function returns the permalink of each post as it’s looping through them. To get the permalink for the page OUTSIDE of the loop you’re currently in, you can use this:

get_permalink( get_queried_object_id() );

get_queried_object_id() will get the ID of the object (post/page/whatever) of the webpage you’re on, then passing that to get_permalink will result in getting it’s permalink. Problem solved!

For one example, I was recently building a site for which I needed to loop through all Staff post type entries and include a button on each of them that if clicked, would submit a form using $_GET back to that same webpage. I used this code to get the correct permalink for the action=”” attribute of the form:

Handy dandy.


  1. Soumya August 4, 2015 at 3:47 pm


    Thank you!!! Victory!! I was going crazy for the last 5 hours trying to figure out how to make my form on a wordpress post return the url in the form submission email sent to me…im a newbie in coding…so it was difficult for me to figure out the error…this solved my problem thank you so much!

    • Kellen Mace August 5, 2015 at 9:04 am


      Great! You’re very welcome. Glad this post helped you out 🙂

  2. Williams November 5, 2017 at 1:28 am


    whoa, thanks a lot for this, it really helps and cover what i need, Gratitude

  3. ProDesignTools December 7, 2018 at 6:52 pm


    Thanks, was wondering the same thing. But in your use case, actually less could be more… If you just use action=”” in your form, then that will have the effect of looping back to the same page with your results. Nothing else would be needed.

    Or you could set and use an anchor on the page, such as action=”#results”. For more discussion, see:


    This approach has the added benefit of working on WordPress archive and index pages, where the get_permalink() will not be accurate.

    Nevertheless, it can still definitely be handy to know the permalink of a page outside the loop. Just be aware that this other, simpler approach may work better in your case.

Leave a reply

Your email address will not be published. Required fields are marked *