WordPress get_permalink() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns a URL

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().

WordPress
<?php echo esc_url( get_permalink() ); ?>
CallWhat 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
WordPress
get_permalink( int|WP_Post $post = 0, bool $leavename = false ): string|false

Parameters

ParameterDescription
$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 ValueDescription
stringThe permalink of the post.
falseIf the post cannot be found.

WordPress Examples

Three short examples. Each one shows the code and what it prints or does.

Example 1 — Link a title in the Loop

Use the current post. No ID is needed.

WordPress
<?php
while ( have_posts() ) {
  the_post();
  echo '<a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a>';
}

How It Works

  1. The Loop sets the current post.
  2. get_permalink() returns its URL.
  3. 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.

WordPress
<?php
$url = get_permalink( 42 );
if ( $url ) {
  echo '<a href="' . esc_url( $url ) . '">Read the guide</a>';
}

How It Works

  1. 42 is the post ID.
  2. If the post is missing, the function returns false.
  3. 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.

WordPress
<?php
echo esc_url( get_permalink( 42, true ) );

How It Works

  1. The second argument is $leavename.
  2. With true, the post name is not filled in.
  3. Normal themes use the default false.

Frequently Asked Questions

It returns the full URL of a post, page, or other post type.
the_permalink() prints the URL. get_permalink() returns it, so you can store it or build HTML around it.
Yes, when you print it into HTML. get_permalink() does not escape the URL. the_permalink() escapes it for you.
When the post cannot be found, for example if the ID is wrong.
Yes. It works for posts, pages, and most custom post types.

Next: get_post_permalink()

Return the permalink of a post, built for custom post types.

get_post_permalink() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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