2

I have been following the steps on the answer here and it worked wonders.

But my problem is on this part,

on the original:

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
    $newRules  = array();
    $newRules['custom-post-type-base/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?custom_post_type_name=$matches[4]'; // my custom structure will always have the post name as the 5th uri segment
    $newRules['custom-post-type-base/(.+)/?$']                = 'index.php?taxonomy_name=$matches[1]'; 

    return array_merge($newRules, $rules);
} 

my version:

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
    $newRules  = array();
    $newRules['custom-post-type-base/(.+)/(.+)/(.+)/?$'] = 'index.php?custom_post_type=$matches[3]'; 
    $newRules['custom-post-type-base/(.+)/?$']           = 'index.php?custom_taxonomy=$matches[1]';

    return array_merge($newRules, $rules);
} 

Since my taxonomy has two levels, it couldn't recognize whether the 2nd param (custom-post-type-base/(.+)/[here]/) is a custom post or a custom taxonomy.. it returns correctly on custom taxonomy but returns 404 on a custom post type with the post type being categorized on the parent taxonomy.

See Sample URLS:

"/custom-post-type-base/taxonomy-parent/taxonomy-child/single-custom-post-type/"
/*works as single custom post*/

"/custom-post-type-base/taxonomy-parent/taxonomy-child/"
/*works as custom taxonomy page*/

"/custom-post-type-base/taxonomy-parent/"
/*works as custom taxonomy page too*/

"/custom-post-type-base/taxonomy-parent/single-custom-post-type/"
/*returns a 404*/
8
  • have you flushed rewrite rules?
    – Tom J Nowell
    Jun 7, 2013 at 14:59
  • yes, im doing it everytime i update my code.. Jun 7, 2013 at 15:01
  • Does it match in monkeyman rewrite analyser?
    – Tom J Nowell
    Jun 7, 2013 at 15:02
  • what is that? o.O? it matches the taxonomy part, the problem is if instead of a taxonomy a custom post is in the parameter.. so i need a switch that would allow both custom tax and custom post type on the 2nd param #basename/(.+)/[here]/(.+)/?$# Jun 7, 2013 at 15:03
  • in other words, the custom post type can be both on the 2nd and 3rd param, the taxonomy is two levels so adding the post type in the child taxonomy works, but adding it to the parent taxonomy would render a 404.. Jun 7, 2013 at 15:06

1 Answer 1

1

You have:

  • A rewrite rule that handles the post being in the 5th part
  • A rewrite rule to handle the taxonomy part

A rewrite rule to handle the post being in the 2nd or 3rd part isn't in the above list, hence why you get a 404.

If we take this rewrite rule:

$newRules['basename/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?custom_post_type_name=$matches[4]'; // my custom structure will always have the post name as the 5th uri segment

And modify it slightly using some common sense, namely:

  • basename/(.+)/(.+)/(.+)/(.+)/?$ ....

    custom_post_type_name=$matches[4]

    // my custom structure will always have the post name as the 5 th uri segment

  • basename/(.+)/(.+)/(.+)/?$ ....

    custom_post_type_name=$matches[3]

    // my custom structure will always have the post name as the 4 th uri segment

  • basename/(.+)/(.+)/?$ ....

    custom_post_type_name=$matches[2]

    // my custom structure will always have the post name as the 3 th uri segment

etc

Place these in ascending order prior to your existing rule, and repeat as many times as is necessary

2
  • ooops, i've already adjusted my code from the original (will post my code on top), i dont get a 404 on the 2nd and 3rd sample url. please see. thanks! Jun 7, 2013 at 15:29
  • the only time i get a 404 is when the post is under the parent taxonomy, if its under the child taxonomy everything is fine.. Jun 7, 2013 at 15:32

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.