get_post_permalink

Definition:
function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {}

Retrieve the permalink for a post with a custom post type.

Parameters

  • int $id: Optional. Post ID.
  • bool $leavename: Optional, defaults to false. Whether to keep post name.
  • bool $sample: Optional, defaults to false. Is it a sample permalink.

Defined filters

  • post_type_link
    apply_filters('post_type_link', $post_link, $post, $leavename, $sample)

Source code

function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {

	global $wp_rewrite;



	$post = &get_post($id);



	if ( is_wp_error( $post ) )

		return $post;



	$post_link = $wp_rewrite->get_extra_permastruct($post->post_type);



	$slug = $post->post_name;



	$draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );



	$post_type = get_post_type_object($post->post_type);



	if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {

		if ( ! $leavename ) {

			if ( $post_type->hierarchical )

				$slug = get_page_uri($id);

			$post_link = str_replace("%$post->post_type%", $slug, $post_link);

		}

		$post_link = home_url( user_trailingslashit($post_link) );

	} else {

		if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )

			$post_link = add_query_arg($post_type->query_var, $slug, '');

		else

			$post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');

		$post_link = home_url($post_link);

	}



	return apply_filters('post_type_link', $post_link, $post, $leavename, $sample);

}

1603

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: