Custom post type permalinks giving 404s

My plugin Custom Post Permalinks does this sort of thing for you:

http://wordpress.org/extend/plugins/custom-post-permalinks

If you’d rather use your own solution, I’ll need a bit more information, such as the registration code for the forums taxonomy.

Just guessing, I’m thinking the regex for a forum looks identical to the regex for a topic to the rewrite engine.

EDIT

Looking at your code, it looks like you’re using 3.1. The $args['rewrite']['hierarchical'] bit for taxonomies wasn’t in 3.0. Basically, what that argument does is changes the regular expression for the %forum% tag from ([^/]) to (.+?). This means that WordPress’ rewrite engine gets a match for this regular expression first:

 @/(.+?)/(.+?)/?$

Which, for something like /generic-forum/specific-forum/specific-topic, will parse to this:

index.php?forum=generic-forum&topic=specific-forum/specific-topic

To test if this is really what’s causing the 404, change the taxonomy’s rewrite args so that ['rewrite']['hierarchical'] is either not set or set to false, flush the rewrite rules, and modify your topic link function not to add parents to the link; Then test to see if the new links work.

If this is the cause of the problems, there are a couple of ways to fix this. The easiest would be to add a bit of plain text to the permastruct, like this: %forum%/topic/%topic%. That would give you links like this: /general-forum/sub-forum/topic/whatever. Another way would be to add another rewrite tag like this:

$wp_rewrite->add_rewrite_tag( '%post_type_topic%', '(topic)', 'post_type=" );

then change the permastruct to “%forum%/%post_type_topic%/%topic%’.

Leave a Comment