0

I am creating a custom tours and travel theme in which I have created custom post type Cities, Locations, Destinations and i am using custom permalink structure. /%category%/%postname%/

So now I want to append a state name or district name before that city, destination or location name. Let me make it more clear by giving you an example .

The current url structure is like this. http://test.com/custom_post_type_slug/post_name_slug

And how I want it to be like this. http://test.com/Custom_dynamic_string/custom_post_type_slug/post_name_slug Right now i am doing something like this

function add_places_rewrite_tags(){
add_rewrite_tag('%district%','([^/]+)');
}
add_action( 'init', 'add_places_rewrite_tags' );

add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
    "/([^/]+)/hotel/([^/]+)/?",
    'index.php?post_type=hotel&district=$matches[1]&p=$matches[2]',
    'top'
);
add_filter( 'query_vars', 'custom_query_vars' );
}
function custom_query_vars( $query_vars )
{
   $query_vars[] = 'district';
   return $query_vars;
}

but it's still not giving me the desired results and i am also using a plugin to analyze the re-wright rules

1

try this

add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
function my_insert_rewrite_rules( $rules )

{
    $newrules = array();
    $newrules['(.+?)/custom_post_type_slug/(.+?)/?$'] = 'index.php?post_type=custom_post_type_slug&dynamic_string=$matches[1]&custom_post_type_slug=$matches[2]';
    return $newrules + $rules;
}

function my_insert_query_vars( $vars )
{
    array_push($vars, 'dynamic_string');
    return $vars;
}
1
  • Thanks deepa. I am doing it something like this I have this url abc.com/hotel/new_view but i want to insert a new string which will be its district which is saved in its meta-field so the url will become something like this abc.com/shimla/hotel/new_view Apr 1 '13 at 10:06
0

After adding this to functions.php and customizing it toward your needs, be sure to visit Settings > Permalinks in the admin to flush the rewrites.

add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules()
{
    add_rewrite_rule(
        "/([^/]*)/custom_post_type_slug/([^/]*)/?",
        'index.php?post_type=custom_post_type_slug&Custom_dynamic_string=$matches[1]&custom_post_type_slug=$matches[2]',
        'top'
    );

    /**
     * Add the above to Query Vars
     */
    add_filter( 'query_vars', 'custom_query_vars' );
}

function custom_query_vars( $query_vars )
{
    $query_vars[] = 'Custom_dynamic_string';
    return $query_vars;
}

Then, in your templates, you can get the "Custom_dynamic_string" (or whatever you decide to call it) variable using this function:

$custom_dynamic_string = get_query_var('Custom_dynamic_string');
2
  • Thanks jake. I am doing it something like this I have this url abc.com/hotel/new_view but i want to insert a new string which will be its district which is saved in its meta-field so the url will become something like this abc.com/shimla/hotel/new_view Apr 1 '13 at 10:05
  • You should be able to use the above code to do that. And then when you link to it, you would have to build your links manually: $url = site_url() .'/'. $meta_value .'/'. $post->post_type .'/'. $post->post_name .'/'; And on the page you could get the meta_value to filter things and check against the stored meta value...
    – Jake
    Apr 1 '13 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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