post_type_link │ filter-hook │ WP 3.0.0

Allows changing the permalink for a custom post.

The callback must return a URL.

The filter is called at the end of get_post_permalink(), in the wp-includes/link-template.php file. get_permalink() also uses this function for custom post types.

When retrieving links through get_permalink(), the following filters are used:

Usage

add_filter( 'post_type_link', 'wp_kama_post_type_link_filter', 10, 4 );

/**
 * Function for `post_type_link` filter-hook.
 * 
 * @param string  $post_link The post's permalink.
 * @param WP_Post $post      The post in question.
 * @param bool    $leavename Whether to keep the post name.
 * @param bool    $sample    Is it a sample permalink.
 *
 * @return string
 */
function wp_kama_post_type_link_filter( $post_link, $post, $leavename, $sample ){
	// filter...
	return $post_link;
}
$post_link(string)
Post permalink.
It may be a pretty permalink or a URL with GET parameters: the filter applies in both cases.
$post(WP_Post)
Object for the post whose link is being generated.
Check $post->post_type to limit the filter to the required post type.
$leavename(boolean)
Whether to keep the %post_type% placeholder in the pretty permalink instead of replacing it with the post slug.
For example, when true, the link may contain /books/%book%/.
When false, WordPress inserts the slug, or the path with parent posts for a hierarchical post type.
This flag does not affect the result when generating a regular link with GET parameters.
$sample(boolean)
Whether a sample permalink is being generated, for example, for the editor. This is not a check for preview mode with is_preview().
When true, WordPress may use a pretty permalink even for a post that would normally need a plain link.
get_sample_permalink() temporarily changes the post object's status and slug. Therefore, check $sample, not only $post->post_status, to exclude sample permalinks.

Examples

#1 External link from a meta-field

For published resource posts, replace the link with the value of the external_url meta-field.

add_filter( 'post_type_link', 'my_resource_external_link', 10, 4 );

function my_resource_external_link( $post_link, $post, $leavename, $sample ) {
	if (
		'resource' !== $post->post_type
		|| 'publish' !== $post->post_status
		|| $leavename
		|| $sample
		|| 'link' !== get_post_format( $post )
	) {
		return $post_link;
	}

	$url = get_post_meta( $post->ID, 'external_url', true );
	$url = is_string( $url ) ? esc_url_raw( $url, [ 'http', 'https' ] ) : '';

	return $url ?: $post_link;
}

The replacement works only for the link post format. The post type must support post-formats, and the theme must support the link format if it is selected in the editor.

Redirects from the old format must be handled separately on template_redirect. Check the same post type, status, format, and meta-field; exclude previews with is_preview().

Use wp_redirect() to redirect to an external site, and call exit after a successful redirect.

#2 Pass a GET parameter into the link

Add the current utm_source to links for book posts. Only the selected parameter is passed through to avoid accidentally adding internal request parameters.

add_filter( 'post_type_link', 'my_book_tracking_link', 10, 4 );

function my_book_tracking_link( $post_link, $post, $leavename, $sample ) {
	if ( 'book' !== $post->post_type || $leavename || $sample ) {
		return $post_link;
	}

	if ( ! ( $_GET['utm_source'] ?? '' ) ) {
		return $post_link;
	}

	$source = sanitize_text_field( wp_unslash( $_GET['utm_source'] ) );

	return add_query_arg( 'utm_source', rawurlencode( $source ), $post_link );
}

add_query_arg() does not escape the result for HTML. Use esc_url() when outputting the link yourself in href.

Keep in mind that the filter applies to all matching calls that retrieve a link, including calls outside a template. If the parameter is needed only for a specific button, add it when generating that button.

#3 Remove the post type prefix

For example, you can change /team-member/jack/ to /jack/. However, changing the string in post_type_link alone is not enough.

You also need to teach WordPress to find the correct post type at the new URL. If rewrite rules have already parsed the URL into the name query variable, you can refine the main query with pre_get_posts: find the post with get_page_by_path() and set its post_type.

Limit this handling to the main front-end query when no post_type is explicitly set. If the URL is parsed differently, for example into pagename, separate handling is required. You also need to resolve conflicts with pages and regular posts that have the same slug. Removing the prefix universally without accounting for the site structure is not possible.

Changelog

Since 3.0.0 Introduced.

Where the hook is called

get_post_permalink()
post_type_link
wp-includes/link-template.php 375
return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );

Where the hook is used in WordPress

Usage not found.