Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use post_type_link filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 9, 2022
5 minutes read

post_type_link filter

Filters the permalink for a post of a custom post type.

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


This is filter hook , its filter the permalink for a post of a custom post type.
By using the hook , we can append / customize the link/url of post.


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

Live Example

To run the hook, copy the example below.

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

The following example is for adding a hook callback.

// define the post_type_link callback 
function filter_post_type_link( $post_link, $post, $leavename, $sample ) { 
    // make filter magic happen here... 
    return $post_link; 
// add the filter 
add_filter( 'post_type_link', 'filter_post_type_link', 10, 4 );  

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'post_type_link', 'filter_post_type_link', 10, 4 ); 
function append_query_string( $url, $post ) {
    if ( 'my_custom_post_type' == get_post_type( $post ) ) {
        return add_query_arg( $_GET, $url );
    return $url;
add_filter( 'post_type_link', 'append_query_string', 10, 2 );
Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.