Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

get_permalink not fetching correct pages

get_permalink will fetch the URL of the current post within a loop. If you call it on an archive page, then what WP thinks is the “current post” is actually the first post that you are about to loop through and so you end up with that post’s permalink rather than the current page’s URL.

I’ve not found a better way than this, which effectively rebuilds the URL from the current request:

global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );

Doing it this way rather than messing with $_SERVER, which I’ve seen in various online posts, at least ensures that you easily get your canonical hostname & your protocol right.

Related Posts:

  1. Check if wp-login is current page
  2. Associate Page with User
  3. How to redirect login and register pages
  4. Could be a namespace conflict (?) with login form and register form on the same page
  5. Edit page shown after login in WordPress?
  6. Bad pages redirection
  7. logout redirect only from admin page
  8. 404.php only works if I’m logged in. Everybody else redirects to wp-login.php – why?
  9. Custom page only editable by one user
  10. WordPress redirects to login page when I enter url.com/index.php
  11. Make wordpress pages accessible for Woocommerce logged in users only
  12. Post id(login page) not found
  13. How can a user login on a wordpress page?
  14. Add infinite scroll to content splitted post
  15. How to get current page ID outside the loop?
  16. Contact Form on WordPress Sites?
  17. Page returns 404 with POST variables, but not without
  18. Custom templates not showing up in template dropdown
  19. Get page id by template
  20. Using pre_get_posts on true pages and static front pages
  21. Get the content of a specific page (by ID)
  22. How to get page title with the page ID?
  23. Show more than 20 items in pages or posts edit dashboard
  24. Allowing user to edit only certain pages
  25. How to display last 3 posts (recent posts) in a static page?
  26. How to give paged links custom title?
  27. How to set post slug when using wp_insert_post();?
  28. How can I keep the content of my pages version controlled?
  29. How to load javascript on custom page template?
  30. How reduce wordpress login session timeout time?
  31. How to configure WordPress to handle 75,000 pages?
  32. How to update page status from publish to draft and draft to publish
  33. Check if is on child-page of a particular page
  34. Change page template programmatically ?
  35. Conditional tag to check if ‘page.php’ is being used?
  36. Customizing the URLs of WordPress Login and Sign-up Pages?
  37. How to disable posts and use pages only
  38. featured image as background image on pages
  39. Check IF is a “single product page” and Check the “role” for a Redirect
  40. A check for if is parent page, if has children, if has grandchildren
  41. How to load JS and CSS only on specific Pages using is_page()?
  42. Listing pages which uses specific template [duplicate]
  43. Proper way to get page content
  44. Access the same page from multiple urls (wildcard)
  45. Create a page without adding a page in the Database
  46. Is there a way to change the default page template selection?
  47. Can I have two child pages of different parents with the same name?
  48. What methods to use to create small, editable pieces of text for static pages?
  49. Show Default Editor on Blog Page ( Administration Panel )
  50. Hide page visual editor if certain template is selected?
  51. What is the difference between $paged and $page?
  52. multi page password protection
  53. Create a “Dummy” parent page for a hierarchy in page listing?
  54. How do I convert a page’s title to lower case?
  55. Get top level page parent title
  56. How to add a specific widget to only 1 page?
  57. Restrict admin access to certain pages for certain users
  58. page title, parent tilte and grand parent title
  59. How to change the default logout link on WordPress Admin
  60. How to add jQuery script to an individual page?
  61. WordPress page edit does not save selected template
  62. Add a Page without header and menus?
  63. What is an alternative to get_page_by_title()?
  64. Password protect page with multiple passwords
  65. Create pages automatically if they don’t exist
  66. Find empty pages or pages which have less than x characters
  67. Allow only new sub-pages to be created
  68. Set page template automatically based on parent
  69. how to display the wordpress login and register forms on a page?
  70. Editing Complex Pages in Visual Mode
  71. Link section within page with slash (/) and not hash (#) without reloading the page
  72. Creating a template using a specific slug name, for a page with a parent page
  73. Can I display submenus in groups, using wp_list_pages?
  74. Is there a default template file for child pages / subpages?
  75. Get wp_get_attachment_url outside of loop
  76. Order by menu_order and title?
  77. Require title for pages
  78. WP Page and Subdirectory with same name
  79. Replace Dashes Before Title in Page List
  80. Add a new page to wordpress programmatically
  81. Get Permalink for the top level parent of child pages
  82. Proper method to restrict non logged users into certain pages
  83. How to redirect on particular page in wordpress? [closed]
  84. Get page id by title?
  85. Masking logout URL
  86. What is the use of $page_title and how to use it?
  87. How to embed page content in a blog post
  88. Can I display the widget admin in the page admin?
  89. handling login/logout redirects
  90. How to add or remove metabox each page separately?
  91. Some pages are missing from the Parent Page select in the Editor
  92. WordPress parent select need to be removed
  93. Manual excerpts for pages not working on Search
  94. Prevent WordPress from adding linebreaks to javascript embedded in a page
  95. How to get the specific Page’s current ID and use it in a get_page function?
  96. Custom SQL Query for WordPress page
  97. How to display code if it is not certain pages?
  98. share login/logout sessions across two installs?
  99. Change admin startpage to Pages-page?
  100. Notify admin when page is edited?
Categories pages Tags login, logout, pages
How to generate feed for custom template
Not showing old migrated content in website

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress