Make custom user_meta as permalink for post

After searching a lot i finally came up with this solution.

My custom post type is “facility” and the custom_user_meta=”organization_name“.

First go to http://localhost/wp-admin/options-permalink.php and choose custom structure – in the text box put /%organization_name%/%postname%/

After that put the below given code in functions.php

add_action('init', 'tdd_add_rewrite_rules2');
function tdd_add_rewrite_rules2(){
// Register custom rewrite rules
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag('%facility%', '([^/]+)', 'facility=');
$wp_rewrite->add_rewrite_tag('%organization_name%', '([^/]+)', '');
$wp_rewrite->add_permastruct('facility', '/%organization_name%/facility/%facility%', false);
}

add_filter('post_type_link', 'tdd_permalinks2', 10, 3); 
function tdd_permalinks2($permalink, $post, $leavename){ 
$no_data = get_the_author_meta('ID');;
$post_id = $post->ID;
if($post->post_type != 'facility' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))  return $permalink;
// $var1 = get_post_meta($post_id, 'posts_solicitorspeciality', true);
$var1 = get_the_author_meta('organization_name');

$var1 = sanitize_title($var1);
if(!$var1) { $var1 = $no_data; }
$permalink = str_replace('%organization_name%', $var1, $permalink); 
return $permalink; 
}