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 have a page created (page ID 235) with a page template that will need to be passed a variable. I would like to pass this variable through the URL. My first attempt was to pass this with a GET variable, and that worked just fine.

URL: http://example.com/sok/events/?kategori=variablehere

Now, I thought I'd spice this up a bit and prettify it. This is what I am aiming to achieve:

URL: http://example.com/sok/events/variablehere

The code I have been using in my attempt looks like the following:

function setup_filter_rewrites()
{
    add_rewrite_tag('%kategori%','([^&]+)');
    add_rewrite_rule('^sok/events/([^/]*)/?', 'index.php?p=235&kategori=$matches[1]', 'top');
}
add_action( 'init', 'setup_filter_rewrites' );

Now, when accessing the URL above, I am redirected to http://example.com/sok/events/ - i.e. the URL but without the query variable. Obviously, as you may guess, the query variable is not accessible inside the page.

I heard of the redirect_canonical function potentially causing problems with rewrite rules, so I disabled it. After having done so, accessing the above URL instead yields a 404.php request.

As you can see, I've really tried to cut back on the code to make it as simple as possible as well as using the correct API functions. I have checked the rule with A tool to analyze rewrite rules? and it reports the variable as being public and accessible. I tried cutting the page down to a simple echo $wp_query->query_vars['kategori'] but that did not output anything nor did it prevent the redirection.

I suspect the redirection is the problem here - if I could stop it, I'm confident I'd be able to grab the query variable.

This problem is similar to URL Rewrite + Page + Custom Post Type = Unusual Redirect and Querystring parameter getting lost in rewrite rule insofar that it is also a redirection error. However, in my case, I am not dealing with custom post types of any kind.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I think you just have to add kategori to the array of query vars:

EDIT- after much iteration we've arrived at a solution. the original code seemed to be working, but only with specific permalink settings, as I was mistakenly routing to a post instead of a page. rather than p=, it should be pagename=, with both the parent and child page in the path:

function setup_filter_rewrites()
{
    add_rewrite_rule('sok/events/([^/]*)/?', 'index.php?pagename=sok/events&kategori=$matches[1]', 'top');
}
add_action( 'init', 'setup_filter_rewrites' );  

function setup_filter_query_vars( $query_vars )
{
    $query_vars[] = 'kategori';
    return $query_vars;
}
add_filter( 'query_vars', 'setup_filter_query_vars' );
share|improve this answer
    
I thought that was what add_rewrite_tag did? I tried your code (commenting out my add_rewrite_tag) but to no avail. It also seems the two other questions I've linked haven't been answered - this is somewhat curious! –  Doggie52 Aug 25 '11 at 19:44
    
make sure you flush rewrite rules each time you make a change. the above code pasted in a new install works here without a redirect, so maybe you have some other code or plugin interfering with the query. –  Milo Aug 25 '11 at 19:59
    
are you absolutely totaly 100% sure this worked for you on a fresh install? Do you mind copy-pasting all of the code (including the code for the page template) you used into pastebin or something? I've disabled all of my plugins, even switched back to Twenty Eleven, and made sure to refresh my permalinks every time - it still redirects to the page URL without the appended query variable. I'm beginning to think this is some sort of bug, as I cannot see what is going or wrong or where it happens. –  Doggie52 Aug 26 '11 at 6:59
    
@Doggie52 - ok, I think I've figured out what may be your issue. What are your permalink settings? I can reproduce the redirect if I set permalinks to /%category%/%postname%/. any other permalink setting works except that one. here are the template files I'm using. –  Milo Aug 26 '11 at 18:56
    
well, events is a page and sok is its parent page, so I am not using that particular permalink structure, no. But it somewhat resembles your proposed error-causing structure, I admit that. I'll see what happens when ridding myself of the parent page. In the meantime, let me ponder over this: does your ability to reproduce this error with a very particular permalink structure turn this into a bug? –  Doggie52 Aug 27 '11 at 19:30
show 8 more comments

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.