0

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.

1 Answer 1

2

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!

2
  • Perfect!!! that did work. First I was using [^/]+ then I dropped it because I was not able to get it to work. Thank you. May 20, 2011 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. May 20, 2011 at 10:36

Your Answer

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

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