Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have custom post types registered and they are working fine with their normal URLs. My Custom post types have 'members' as their slug. Now I am trying to have a custom permalink for my custom post types. Here is my $wp_write->add_rule

$wp_rewrite->add_rule('members/.+?/(.+)/$','index.php?post_type=pasha_members&name=$matches[1]', 'top');

Below is my $wp_rewrite->rules array.

array(
    ['members/.+?/(.+)/$'] => 'index.php?post_type=pasha_members&name=$matches[1]'
    ['category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$'] => 'index.php?category_name=$matches[1]&feed=$matches[2]'
    ['category/(.+?)/(feed|rdf|rss|rss2|atom)/?$'] => 'index.php?category_name=$matches[1]&feed=$matches[2]'
   ...
);

And here is the url that I am trying to access, I am not able to figure out what I am doing wrong.

http://wp.dev/members/city1-city2/best-compnay/

When I change Permalink settings to default in Settings->Permalink, I can access the custom post just fine using the following url.

http://wp.dev/?post_type=pasha_members&name=best-compnay

Let me know if you need more information. Any help pointers are much appreciated.

NOTE: I have put the add_rule in init instead of activate just for the testing. I also have put the flush_rewrite_rules in the init, its also temporarily until its fixed.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

It appears that WordPress cuts off the final / before it starts matching the URL. So make it optional in the regex and it should work:

members/.+?/(.+)/?$

If the city and company parts will not contain slashes it could be even more clear to use [^/]+ to match them ("everything up to a slash"). Then your regex would become:

members/[^/]+/([^/]+)/?$

I created a plugin to analyze your rewrite rules, it is really easy to play with and debug your rules. Although you made me discover an error in it: it did not remove the final slash, so your first regex matched in my analyzer but not in the "real" WordPress. I'll update it soon!

share|improve this answer
    
Perfect!!! that did work. First I was using [^/]+ then I dropped it because I was not able to get it to work. Thank you. –  Hameedullah Khan May 20 '11 at 7:47
    
One more question.. I want to reset the rewrite rules.. (i.e want wordpress to forget and then regenerate). so should I switch back to default in permalink settings and then back to custom. I think I messed up something now wordpress is going to home page for every url. –  Hameedullah Khan May 20 '11 at 10:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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