I have created a custom user meta = shop_name in User Profile and custom post type gallery

I want my permalink to be in form of /%shop_name%/gallery/%gallery%.

Every user has a custom user_meta = shop_name.

thus if my user has shop_name=shoppers stop and a gallery whose title is Washing Machine then my user url should be http://localhost/shoppers-stop/gallery/mashing-machine and when i go to http://localhost/shoppers-stop/gallery/ all the galleries of shoppers-stop should be listed.

Below is the code which i have written.

add_action('init', 'tdd_add_rewrite_rules');
function tdd_add_rewrite_rules(){
// Register custom rewrite rules
global $wp_rewrite;
//$wp_rewrite->add_rewrite_tag('%gallery%', '([^/]+)', 'gallery=');
$wp_rewrite->add_rewrite_tag('%shop_name%', '([^/]+)', 'shop_name=');
 $wp_rewrite->add_permastruct('gallery', '/%shop_name%/gallery/%gallery%', false);
add_filter('post_type_link', 'tdd_permalinks', 10, 3); 
function tdd_permalinks($permalink, $post, $leavename){ 
$no_data = get_the_author_meta('ID');;
$post_id = $post->ID;
if($post->post_type != 'gallery' || 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('shop_name');
$var1 = sanitize_title($var1);
if(!$var1) { $var1 = $no_data; }
  $permalink = str_replace('%shop_name%', $var1, $permalink); 
  // $permalink = str_replace('%post_id%', $post_id, $permalink); 
return $permalink; 

Read more here: Custom Post Type with /%shop_name%/gallery/%gallery% permastruct?


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: