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've set my permalinks to a custom structure:

/%menutype%/%postname%/

This works all right for my custom taxonomy (menutype) and anything that's an archive.

Now however, simple pages won't work, i.e.

index.php?page_id=32 -> 404!

or

/about/ ->404

Here are my custom rewrite rules, 'menu' is my custom post, and 'menutype' is my custom taxonomy:

function custom_rewrite( $wp_rewrite ) {
    $feed_rules = array(
        'menu/[a-z]+/(.+)' => 'index.php?menu=' . $wp_rewrite->preg_index(1),
        'menu/([^/]+)(/[0-9]+)?/?$' => 'index.php?menutype=' . $wp_rewrite->preg_index(1),
        '/(.+)/' => 'index.php?page_id=' . $wp_rewrite->preg_index(1),
    );

    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( 'generate_rewrite_rules', 'custom_rewrite' ); 
share|improve this question
    
Whenever you filter the rewrite rules, don't forget to re-save the rules in Permalinks options to flush out the rules. Otherwise you will get 404s even if your code is correct. –  helgatheviking Jan 22 '13 at 4:18
add comment

1 Answer

You need to setup your permalinks for menutype using custom rewrite rules. What you've done is apply the menutype prefix to everything, even though menutype isn't relevant to everything, so things are being misinterpreted.

You would need something similar to this:

function custom_rewrite( $wp_rewrite ) {
    $feed_rules = array(
        '([^/]+)(/[0-9]+)?/?$'    =>  'index.php?menu_type='. $wp_rewrite->preg_index(1).'&post_name='. $wp_rewrite->preg_index(2)
    );

    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( 'generate_rewrite_rules', 'custom_rewrite' );

However, I would recommend you add a prefix such as 'menus' etc beforehand to prevent further clashes with the existing rewrite rules.

Use the monkeyman rewrite analyser tool plugin to test any rewrite rules you make/change/edit by putting in URLs and seeing which rules are activated and in which order.

share|improve this answer
    
I added in what I'm using to my question above...but will take a look at this code too. –  redconservatory Mar 30 '12 at 20:10
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.