Custom permalink that contains post meta information

Here is the example how to improve custom post type permalink by adding additional information from the post meta in here the album name for the gallery. The post type in here is called gallery.

Instead of URL like /gallery/post-name we will have /gallery/album-name/post-name

add_filter('post_type_link', 'f_gallery', 10, 3);
function f_gallery($permalink, $post, $leavename)

  $post_id = $post->ID;
  if($post->post_type != 'gallery' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
    return $permalink;
  $album = get_post_meta($post_id, 'album', true);
  $album = sanitize_title($album);
  if(empty($album)) { $album = "unknown"; }
  $permalink = str_replace('gallery/', 'gallery/'.$album .'/' , $permalink);

  return $permalink;


tags: & category: -