WordPress get_post_permalink() Function

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

Definition

In WordPress, get_post_permalink() returns the URL of a post, built the way custom post types build their links. A custom post type is a content type you add yourself, such as book or portfolio.

For normal posts and pages, most developers use get_permalink(). It already handles custom post types too. Use get_post_permalink() when you want the custom post type link logic on its own.

Syntax

Pass the post ID and print the result with esc_url().

WordPress
<?php echo esc_url( get_post_permalink( 12 ) ); ?>
CallWhat it does
get_post_permalink( 12 )The URL of post 12.
get_post_permalink()The URL of the current post.
get_post_permalink( 12, false, true )A sample URL, useful for drafts.

It returns the URL. Use esc_url() when you print it.

Full declaration
WordPress
get_post_permalink( int|WP_Post $post = 0, bool $leavename = false, bool $sample = false ): string

Parameters

ParameterDescription
$postint|WP_Post. Post ID or object. Default is 0, the current post.
$leavenamebool. If true, keep the name as a placeholder. Default is false.
$samplebool. If true, build a sample URL for a draft or pending post. Default is false.

Return Value

Return ValueDescription
stringThe post URL.

WordPress Examples

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

Example 1 — Get a custom post type URL

Imagine a book custom post type with pretty permalinks.

WordPress
<?php
echo esc_url( get_post_permalink( 12 ) );

How It Works

  1. Post 12 is a book.
  2. The function builds the link from the book rewrite rules.
  3. esc_url() keeps the output safe.

Example 2 — Link a list of custom posts

Run a query for books and link each title.

WordPress
<?php
$books = new WP_Query( array( 'post_type' => 'book', 'posts_per_page' => 3 ) );
foreach ( $books->posts as $book ) {
  echo '<li><a href="' . esc_url( get_post_permalink( $book->ID ) ) . '">' . esc_html( $book->post_title ) . '</a></li>';
}

How It Works

  1. WP_Query finds the latest books.
  2. get_post_permalink() builds the link of each book.
  3. The title is escaped with esc_html().

Example 3 — Get a sample link for a draft

A draft has no real address yet. The third argument asks for a sample.

WordPress
<?php
// Draft book with ID 88
echo esc_url( get_post_permalink( 88, false, true ) );

How It Works

  1. Draft posts do not have a final URL.
  2. true as the third argument asks for a sample URL.
  3. You can show it in a preview box.

Frequently Asked Questions

It returns the URL of a post using the link rules of its post type, which is useful for custom post types.
get_permalink() is the general function and handles all post types. get_post_permalink() builds the link the custom post type way.
Use get_permalink(). It is easier to remember and works almost everywhere.
No. It returns a string, so use echo with esc_url().
It builds a sample URL for a draft or pending post.

Next: get_search_link()

Return the URL of the search results page for a search term.

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

10 people found this page helpful