• Hi,

    I have followed about every bit of advice I can get my hands on but I cannot get add_rewrite_rule to process query variables.

    I have a query variable ‘n’ which I have registered with wordpress. I am using a very simple rewrite just to test it is working.

    If I go to this url manually it works fine:
    index.php?pagename=materials&n=Clay

    But going to:
    resources/materials/Cloth/

    Just takes me to:
    index.php?pagename=materials

    Ignoring the query variable.

    This is the code I have (to test with):

    //Custom Query Vars
    add_filter( 'query_vars', 'add_custom_query_var' );
    function add_custom_query_var( $vars ){
      $vars[] = "n";
      return $vars;
    }
    
    add_action( 'init', 'wpa5413_init' );
    function wpa5413_init()
    {
        add_rewrite_rule(
            // The regex to match the incoming URL
            'resources/materials/Cloth/?',
            'index.php?pagename=materials&n=Clay',
            // This is a rather specific URL, so we add it to the top of the list
            'top' );
    
        add_rewrite_tag('%n%', '([^&]+)', 'n=');
    }
    
    //Flush rewrite rules before adding new ones.
    add_filter('init','flushRules');
    //
    function flushRules(){
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
Viewing 1 replies (of 1 total)
  • Thread Starter spinalwiz

    (@spinalwiz)

    Hi,

    I found the answer. As I was redirecting to a page that was a sub-page of another, the rewrite needed to look like this:

    ‘index.php?pagename=resources/materials&n=Clay’

Viewing 1 replies (of 1 total)
  • The topic ‘add_rewrite_rule Ignoring Query Variables’ is closed to new replies.