add_rewrite_rule not working for me

I have the following rewrite tags defined:

// ADD REWRITE TAG FOR 'VEHICLE MAKES'
add_rewrite_tag('%make%','([^&]+)');

// ADD REWRITE TAG FOR 'BODY STYLES'
add_rewrite_tag('%body-style%','([^&]+)');

// ADD REWRITE TAG FOR 'OTHER TOPICS'
add_rewrite_tag('%topic%','([^&]+)');

I’m trying to create a custom rewrite for each one of these that can apply to any page the variables exist.

Read More

I’ve been reworking the WP example in the documentation with no luck.. This rewrite is for ‘Vehicle Makes’.

I’m aiming for WP to interpret the following:

http://example.com/make/abc

as

http://example.com?make=abc

and the same pattern for

http://example.com?body-style=abc & http://example.com?topic=abc

I can’t seem to get the following to work no matter how much I try

add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');

Any and All help is appreciated!

Related posts

3 comments

  1. Have you tried adding the third parameter to that call?

    add_rewrite_tag( '%make%', '([^/]+)', 'make=' );

  2. The way of using add_rewrite_rule function is given below:

    1. Hook this function into the init hook
    2. Use query_vars filter to make the new rewrite available in the query_vars

    Here is the code snippet

    add_action('init', 'my_custom_rewrite_rule');
    function my_custom_rewrite_rule() {
        add_rewrite_rule( 'make/([^/]+)/?$', 'index.php?make=$matches[1]', 'top' );
    }
    
    add_filter('query_vars', 'my_rewrite_query_var');
    function my_rewrite_query_var( $vars ) {
        $vars[] = 'make';
        return $vars;
    }
    

    Now, go to permalink settings page, and click on Save Changes to flush the rewrite rules.

  3. Before asking any question here, please try to find it out yourself. In case of wordpress you should look at http://codex.wordpress.org/ first.

    Most of the answers are available already.

    The piece of code you are looking for is a function flush_rewrite_rules().

    Ref: Rewrite Rules

Comments are closed.