WordPress get_permalink

Allows you to get the absolute URL of a post, page or custom post type in WordPress. If the first parameter is not specified, then returns the URL of the current post in the WordPress loop.

get_permalink( $post = 0, $leavename = false ) 


$id (integer|WP_Post object) The ID or object of the post to get a link to, defaults to the current post in the loop.

$leavename (boolean) if true, then the link will be returned as a permalink rule

Example 1

Display the URL of the current post in a loop # If you plan to use the function inside the loop, you don’t have to pass the post ID to it, but be sure to use PHP’s echo() function to do the output.

echo '<a href="' . get_permalink() . '">more info</a>';

Example 2

Link to a specific post # Another way to use the function is to pass the ID of the post to which we need to get a link, for example:

 $post_id = 2; $permalink = get_permalink( $post_id );

Example 3

Passing the WP_Post post object to the get_permalink() function # In this example, we used the get_page_by_title() function to get the post object by its title.

echo get_permalink( get_page_by_title( 'WordPress Code' ) ); 

Example 4

Displaying a link to the “Shop” page in WooCommerce # The Store page is a standard WooCommerce storefront page, to get its ID we can use the wc_get_page_id() function, and pass the ID value to the get_permalink() function.

 echo get_permalink( wc_get_page_id( 'shop' ) );

Hooks from function

There are 3 filter hooks in the

get_permalink() function

I will talk about them below. pre_post_link

This filter hook works almost at the very beginning, and allows you to filter exactly the permalink structure obtained from get_option( ‘permalink_structure’ ), but only for the post type.

add_filter( 'pre_post_link', 'true_change_structure', 25, 3 );   
function true_change_structure( $permalink, $post, $leavename ) {   

// we can use the post object to change the structure of a single post e.g. 
if( '531' == $post->ID ) { // if post ID is 531 
$permalink = '/%year%/%monthnum%/%postname%/'; } 
return $permalink;   }


This filter allows you to select a specific category that you would like to use in the link, in case %category% is set in the permalink structure. The first category will be used by default. For a better understanding, here is an example:

add_filter( 'post_link_category', 'true_change_main_category', 25, 3 );   

function true_change_main_category( $cat, $cats, $post ) {   
// by default $cat == $cats[0], i.e. the first element of the array 
// $cat is a WP_Term object, $cats is an array of WP_Term objects to which the post belongs 
// $post is the WP_Post post object   

// if there are more than one rubric and the main rubric with ID=5, then use another one 
if( 5 == $cat->term_id && count( $cats ) > 1 ) { 
$cat = $cats[1]; // another array element 
return $cat;   }


The final filter allows you to filter the resulting post URL.

add_filter( 'post_link', 'true_change_post_link', 25, 3 );   

function true_change_post_link( $permalink, $post, $leavename ) {   

// for example, for posts from the docs category, change links 
if( in_category( 'docs', $post->ID ) { 
$permalink = site_url( 'docs' ) . '#' . $post->post_name; 
 return $permalink; 

Leave a Reply

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

The site was created in 2010, with the goal: to share experiences related to WordPress.

Work on the site does not stop to this day: as forces are added articles, described the core and popular Themes.

Time has shown that the site helps many in their work and study of WordPress.