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

The default URL of the post is http://localhost/gallery/%gallery% which i have modified to below given URL using the code given below.

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

Every user has a custom user_meta = shop_name.

Thus if my user has shop_name="shop3" and a gallery whose title="Washing Machine" then my user URL should be http://localhost/shop3/gallery/mashing-machine

When i go to http://localhost/shop3/gallery/I want all the galleries of shop3 should be listed. Instead the index page is being shown. How can I make it possible?

Below is the code which i have written for creating custom url but want to extend it to above mentioned problem.

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; 
}

This is the User Meta - %shop_name% which i want to be loaded in URL when viewing posts posted by the Author have shop name - fila. I have modified the User Profile settings using ACF to develop this Custom User Settings.

The Default Permalink Structure. enter image description here

Cusotm post - Gallery Posted by user with tilte - gallery 1 enter image description here

The URL of the Custom Post gallery with default permalink structure i.e. http://localhost/gallery/gallery-1/ enter image description here

The Custom URL of the Custom Post gallery with custom permalink structure using the above code i.e. http://localhost/fila/gallery/gallery-1/ enter image description here

When I go to the URL http://localhost/fila/gallery/ i want all the galleries posted by the author with shop_name = fila to be displayed , instead the homepage is being shown. enter image description here

Similarly when I go to the URL http://localhost/fila/ i want the information of shop fila posted by the author to be displayed , instead the homepage is being shown. enter image description here

  • A meta field in URL permastruct? It doesn't sound very intuitive for me. What is supposed that URL should looking for? A page? A custom post type? A user? What is shoppers-stop and how are galleries attached to it? Why do you need a meta field in user profile for this? – cybmeta Mar 19 '15 at 12:23
  • @cybmeta i have modified the content...instead of 'shoppers-stop' i have put the name as 'shop1'. i want to display the different galleries posted only by that particular user – Shahrukh Khan Mar 19 '15 at 12:43
  • shoppers-stop, shop or whatever other name doesn't matter. All the others questions I made to you are important to help you. Please, add that information to your question, otherwise I think it is very difficult to give you an answer, at least for me. – cybmeta Mar 19 '15 at 12:48
  • @cybmeta I have modified the post again with screen shots and hope that i have explained it properly this time so that you can understand – Shahrukh Khan Mar 19 '15 at 14:26
up vote 0 down vote accepted

I used the following functions to solve this problem.

   //This adds a custom query variable to the permalink
   function add_custom_query_var( $vars ){
      $vars[] = "shop_name";
      return $vars;
    }
    add_filter( 'query_vars', 'add_custom_query_var' );

    function add_rewrite_rules($aRules) {
    $aNewRules = array('shop/([^/]+)/?$' => 'index.php?pagename=shop&shop_name=$matches[1]');
    $aNewRules2 = array('shop/([^/]+)/gallery/?$' => 'index.php?post_type=gallery');
    $aRules = $aNewRules + $aNewRules2 + $aRules;
    return $aRules;
    }
    add_filter('rewrite_rules_array', 'add_rewrite_rules');

    //Here we create a custom permalink structure and replace the shop_name with custom user feild value
    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_the_author_meta('shop_name');
        $var1 = sanitize_title($var1);
        if(!$var1) { $var1 = $no_data; }
        $permalink = str_replace('%shop_name%', $var1, $permalink); 
        return $permalink; 
    }
    add_filter('post_type_link', 'tdd_permalinks', 10, 3); 

    function rewrite_flush(){
      global $wp_rewrite;
      $gallery_structure = '/shop/%shop_name%/gallery/%gallery%';
        $wp_rewrite->add_rewrite_tag("%gallery%", '([^/]+)', "gallery=");
        $wp_rewrite->add_rewrite_tag("%shop_name%", '([^/]+)', "shop_name=");
        $wp_rewrite->add_permastruct('gallery', $gallery_structure, false);
      $wp_rewrite->flush_rules();
    }
    add_action('init','rewrite_flush');

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.