WP Snipp Ninja

What the_permalink and get_the_permalink functions are used for in WordPress?

Author: Dimitar Radev | Posted on: December 1, 2020 |

What the_permalink and get_the_permalink functions are used for in WordPress?

In WordPress the_permalink and get_the_permalink functions are used for displaying the post URL also called a permalink. It may be interesting for some of you that there is also a hook called the_permalink and I will do my best to explain what this hook is used for below. Additionally, there are few variations of those functions like get_permalink and get_post_permalink you may stumble upon when browsing the code of WordPress themes.

OK let’s check all the available functions related to getting the post permalink one by one below:

the_permalink Function

We use the_permalink() function when we need to display (echo) the current page or post URL in the template file (example: page.php, single.php). In order for the_permalink() function to work properly, it needs to be placed in the Loop.

Parameters: Accepts Post ID as an integer or WP_Post object, optional. Used in cases when the post object is rewritten and you need to retrieve a different post object from a variable. Defaults to the global $post.

WordPress Codex:  / Reference / Functions / the_permalink()

Sample Usage:

<a href="<?php the_permalink(); ?>">Read More</a>

the_permalink Hook

In WordPress, the_permalink hook is used to filter the current page or post URL before it gets displayed on the page giving you the option to append URL variables for example. the_permalink hook doesn’t filter  get_permalink() or get_the_permalink() functions it works only with the_permalink().

WordPress Codex: / Reference / Hooks / the_permalink

Sample Usage:

function add_query_string($url) {
return add_query_arg($_GET, $url);
add_filter('the_permalink', 'append_query_string');

get_permalink Function

The difference between the_permalink() function and get_permalink() is that the_permalink() retrieves the value without echoing it. It is the preferred way to fetch the permalink in a PHP function. When used outside the loop an ID parameter needs to be provided or the function will retrieve the last post from you posts.

WordPress Codex: / Reference / Functions / get_permalink()


$post – Accepts Post ID or post object, optional. Provides reference post object to pull the permalink from when outside the loop. Defaults to global $post.

$leavename – Boolean, optional. Returns the permastructure rewrite slug instead of the URL.

Sample Usage:

global $post;
$post_URL = get_permalink($post->ID);

get_the_permalink Function

Same function as get_permalink(). get_the_permalink() was added in WordPress 3.9 so the permalink function was consistent with the other post-related template tags prefixed with get_the_.

WordPress Codex: / Reference / Functions / get_the_permalink()

Parameters: Same as get_permalink() function.

Sample Usage:

global $post;
$post_URL = get_the_permalink($post->ID);


Frequently Asked Questions about the_permalink and get_permalink:

How to get the permalink from post ID?

In your fucntions.php file or a PHP function in general it is recommended to assign the returned permalink to a variable from get_the_permalink() or get_permalink() functions. You need to use the $post parameter to pass the post ID.


global $post;
$post_URL = get_the_permalink($post->ID);

How do I get the permalink in WordPress?

It really depends on where do you want to use it. If you are editing a template file and you are in the loop you can just use the_permalink() function. If you are creating a PHP function the preferred way of doing it is to pass the post ID as a parameter in get_the_permalink() or get_permalink(), and assign them to a variable.

What does the_URL function does in WordPress?

In WordPress there is no function called the_URL(). The main URL retrieval functions are:  the_permalink(), get_permalink(), and get_the_permalink().

Why the_permalink is not working in WordPress?

In 90% of the cases, the_permalink function is not working because it’s not in The Loop. Additionally, you can pass the post ID or the $post object to retrieve the proper URL when outside of the loop.

Article keywords

  • About the Author

     Dimitar Radev

    Hey, I'm Dimitar "B1-0S" Radev or D in short. I'm a WordPress Developer for nearly 10 years and I have in-depth knowledge in creating Custom WordPress Themes, Plugins, Commercial Themes Edits, and WooCommerce Storefronts. In the last two years, I'm specializing in Technical SEO for WordPress websites.

    Follow Dimitar Radev on Social Media

    Leave a Reply

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

  • How can i use the the_permalink or get_the_permalink functions outside the loop
    For Example i want to add permalink to the article post at the end of the page like
    Get the Article link and display the full link at the end of the content
    Any ideas how the WordPress Permalink can be used outside the loop and work proper?