Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In order to set up a URL rewrite rule for multiple taxonomy queries, I added this function and these hooks to the function.php file of a site's theme:

function add_custom_rewrite_rule(){
    add_rewrite_rule('(location|event)/(.+)/?$' , 'index.php?$matches[1]=$matches[2]', 'top');
}

add_action('init', 'add_custom_rewrite_rule');
add_action('init', 'flush_rewrite_rules');

It worked but now any link to a single post is broken. I tried removing the code I added and manually flushing permalinks in the admin, but links to single posts are still broken. Any ideas?

Update: I tried manually removing the "rewrite_rule" and "permalink_structure" rows from the "wp_options" table to see if that would force WordPress to update them when I changed the Permalink Settings in the admin, but the table didn't update when I did that.

I've also tried this call:

add_option("permalink_structure", "%year%/%monthnum%/%day%/%postname%/", "no");

And still saw no change in behavior or anything. Then I tried manually adding the row back to the database:

$get_wp_options_table = mysql_query("INSERT INTO wp_options (option_name, option_value, autoload) VALUES ('permalink_structure', '/%year%/%monthnum%/%day%/%postname%/', 'no')");

And the insert worked but again, no change in behavior whatsoever. Links to single posts still 404.

share|improve this question
What is your permalink structure? – Matthew Boynes Jan 31 '12 at 4:17
try going to your permalink page to flush the rewrites (just visit the permalink page) – Sisir Jan 31 '12 at 9:50
bad practice - don't hook the flush_rewrite_rules to init. – amit Jul 30 '12 at 6:54

closed as too localized by toscho Dec 15 '12 at 23:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

You need to call the flush_rewrite_rules() function within your add_custom_rewrite_rule() so it looks like:

function add_custom_rewrite_rule(){
    add_rewrite_rule('(location|event)/(.+)/?$' , 'index.php?$matches[1]=$matches[2]', 'top');

flush_rewrite_rules();
}

add_action('init', 'add_custom_rewrite_rule');

After some issues with lots of custom post types in 1 WordPress site I found the best solution is to flush when you activate your theme:

function flushRules(){
    global $pagenow, $wp_rewrite;

    if('themes.php' == $pagenow && isset( $_GET['activated'])){
        $wp_rewrite->flush_rules();
    }
}

add_action('load-themes.php', 'flushRules');
share|improve this answer
2  
Do you really want to flush_rewrite_rules() every time init fires? – Chip Bennett Jan 31 '12 at 15:43
It's the only way I've found it to stop errors. – Alex Older Feb 1 '12 at 9:46
Once the custom rewrite rule has been added, and the rewrite rules flushed once, there should no longer be any errors. – Chip Bennett Feb 1 '12 at 13:16
why don't you just resave your permalink structure, that'd flush the rewrite rules. – amit Jul 30 '12 at 6:50
Because this option does it without you having to do it – Alex Older Nov 14 '12 at 9:34

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