post_link filter

Filters the permalink for a post.

apply_filters( 'post_link', string $permalink, WP_Post $post, bool $leavename )


This hook is used for filter the permalink for a post. Its applies only to posts with default post_type of ‘post’, and can’t be applies to custom posts.

post_link is a filter applied to the permalink URL for a post prior to returning the processed url by the function get_permalink().

This filter only applies to posts with post_type of ‘post’. For that filter which applies to custom post type look post_type_link.


  • $permalink : (string) The post’s permalink.
  • $post : (WP_Post) The post in question.
  • $leavename : (bool) Whether to keep the post name.

Live Example

To run the hook, copy the example below.

 $permalink = apply_filters( 'post_link', $permalink, $post, $leavename ); 
if ( !empty( $permalink ) ) { 
   // everything has led up to this point... 

The following example is for adding a hook callback.

// define the post_link callback 
function filter_post_link( $permalink, $post, $leavename ) { 
    // make filter magic happen here... 
    return $permalink; 
// add the filter 
add_filter( 'post_link', 'filter_post_link', 10, 3 ); 

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'post_link', 'filter_post_link', 10, 3 ); 

 Subscribe to our weekly newsletter below and never miss the latest updates in WordPress