Home WordPress Links Functions get_permalink() WordPress get_permalink() Function Definition In WordPress, get_permalink() returns the URL of a post, page, or other post type. Use it when you want to store the link, change it, or place it inside your own HTML.
Its twin, the_permalink() , prints the URL. Remember it this way: the_ prints, get_ returns.
Syntax Return the URL, then print it with esc_url().
<?php echo esc_url( get_permalink() ); ?> Call What it does get_permalink()The URL of the current post. get_permalink( 42 )The URL of the post with ID 42. get_permalink( 42, true )Keep the post name as a placeholder.
It returns a string. It prints nothing by itself.
Full declaration get_permalink( int|WP_Post $post = 0, bool $leavename = false ): string|false Parameters Parameter Description $postint|WP_Post. A post ID or object. Default is 0, which means the current post. $leavenamebool. If true, the post name stays as a placeholder such as %postname%. Default is false.
Return Value Return Value Description string The permalink of the post. false If the post cannot be found.
WordPress Examples Three short examples. Each one shows the code and what it prints or does.
1. Link a title in the Loop 2. Get the URL of a post by ID 3. Keep the post name as a placeholder Example 1 — Link a title in the Loop Use the current post. No ID is needed.
<?php
while ( have_posts() ) {
the_post();
echo '<a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a>';
} <a href="https://example.com/hello-world/">Hello world</a> How It Works The Loop sets the current post. get_permalink() returns its URL.esc_url() and esc_html() keep the output safe.Example 2 — Get the URL of a post by ID Pass an ID and check that the post exists.
<?php
$url = get_permalink( 42 );
if ( $url ) {
echo '<a href="' . esc_url( $url ) . '">Read the guide</a>';
} <a href="https://example.com/wordpress-hooks-guide/">Read the guide</a> How It Works 42 is the post ID.If the post is missing, the function returns false. The if keeps a broken link from printing. Example 3 — Keep the post name as a placeholder Use leavename when you need to edit the name part later.
<?php
echo esc_url( get_permalink( 42, true ) ); https://example.com/%postname%/ How It Works The second argument is $leavename. With true, the post name is not filled in. Normal themes use the default false. Frequently Asked Questions What does get_permalink() do? It returns the full URL of a post, page, or other post type.
What is the difference between the_permalink() and get_permalink()? the_permalink() prints the URL. get_permalink() returns it, so you can store it or build HTML around it.
Do I need esc_url() with get_permalink()? Yes, when you print it into HTML. get_permalink() does not escape the URL. the_permalink() escapes it for you.
When does get_permalink() return false? When the post cannot be found, for example if the ID is wrong.
Does it work for pages? Yes. It works for posts, pages, and most custom post types.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful
Helpful Share Copy link Suggestion