Basicly I want to indroduce a custom heirarchy with custom post types. I Would like it to be like this.

The Main Brands URL : http://localhost/brands/ which lists different types of Brands.

The personal Brand URL : http://localhost/brands/nike which displays overview of brand "nike" andlinks to different post types made by that particular Brand Account.

The Brand Gallery URL : http://localhost/brands/nike/gallery which is the archive page for custom post type "Gallery".

The Gallery URL : 'http://localhost/brands/nike/gallery/posttitlewhich displays the custom post tupe"gallery"`.

Similarly with custom post type "Information" and "Brochure" the links should be http://localhost/brands/nike/information/posttitle and http://localhost/brands/nike/brochure/posttitle.

I have Implemented the basic structure for permalink of custom post type but have not figured out how to develop the archive page.

I would like to know how to implement the acrhive page - http://localhost/brands/ and http://localhost/brands/nike.

Below is the code for permalink of custom Post type "gallery".

  add_action('init', 'tdd_add_rewrite_rules');
  function tdd_add_rewrite_rules(){
    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag('%brand_name%', '([^/]+)', '');
    $wp_rewrite->add_permastruct('gallery', '/brands/%brand_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_the_author_meta('brand_name');

  $var1 = sanitize_title($var1);
  if(!$var1) { $var1 = $no_data; }

  $permalink = str_replace('%brand_name%', $var1, $permalink); 
  return $permalink; 
  • In your CPT creation you need to have 'has_archive' => true set for archives to work. Then you make the template archive-posttype.php where posttype is the name of the post type.
    – Aibrean
    Jul 28, 2015 at 12:23
  • @Aibrean yes i have done it....but the permalink is http://localhost/gallery/[gallery_name] and i want it in the form of http://localhost/hotel/[hotel_name]/gallery/[gallery_name] Jul 28, 2015 at 14:20
  • More over i want to create a new archive page on the link http://localhost/hotel/[hotel_name] where [hotel_name] is usermeta and is unique for each user. Jul 28, 2015 at 14:23

1 Answer 1


My problem was solved with this link. It was very helpful and difficult to find.

function add_custom_query_var( $vars ){
  $vars[] = "brand_name";
  return $vars;
add_filter( 'query_vars', 'add_custom_query_var' );

function add_rewrite_rules($aRules) {
$aNewRules = array('brand/([^/]+)/?$' => 'index.php?pagename=hotel&brand_name=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
add_filter('rewrite_rules_array', 'add_rewrite_rules');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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