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 that I access at /category/tasks and the file is located at wp-content/themes/my-theme/tasks.php. I would like to make it so I can add a flag after tasks and pick up the query strong in tasks.php. This works fine when I access the page with /category/tasks/?when=upcoming.

Can someone tell me how to use rewrite_rules_array so it will send the variable through the query string using the URL structure /category/tasks/upcoming?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Try this:

function when_rewrite_rules( $wp_rewrite ) {
  $new_rules = array();
  $new_rules['category/tasks/(\d*)$'] = 'index.php?when=$matches[1]';
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules','when_rewrite_rules');
share|improve this answer
    
I'm getting a "Fatal error: Unsupported operand types" error for the line that starts off with $wp_rewrite->rules. Any ideas? –  user1462 Feb 7 '11 at 16:19
1  
@בניית אתרים: You use the filter query_vars, which acts on an array of query vars in the WP class, call the function author_rewrite_rules, which is also the name of a filter that acts on an array of rewrite rules in the WP_Rewrite, and in the end you do something with $wp_rewrite? –  Jan Fabry Feb 7 '11 at 17:11
    
@user1462 - my bad i updated the code as Jan Fabry brought to my attention that i posted the wrong filter and also changed the function name to avoid conflicts. –  Bainternet Feb 7 '11 at 17:18
    
@בניית אתרים: Seeing the other answer you gave makes this clear for me. I thought you combined these three things out of the blue, and I just could not understand how you did that! :-) –  Jan Fabry Feb 7 '11 at 17:27
    
@Jan Fabry - :) Nope it was just a wrong copy paste on my end that's all +1 for picking the up and user1462 said that he can accept when as argument already. –  Bainternet Feb 7 '11 at 17:29
add comment

It's not very hard. You add a new rewrite rule that accepts your URL format and sets the correct query vars (you can choose any name you want, as long as it is unique). There are many ways to add rewrite rules, I prefer add_rewrite_rule(). Be sure to flush the rules once after you do this (from your code, or by visiting the Permalinks page).

add_action( 'init', 'wpse8764_init' );
function wpse8764_init()
{
    add_rewrite_rule( 'category/tasks/upcoming/?$', 'index.php?pagename=category/tasks&wpse8764_var=upcoming', 'top' );
}

This query variable will not be passed on to $wp_query unless you add it to the public query vars. After this, you can access it with get_query_var( 'wpse8764_var' ).

add_filter( 'query_vars', 'wpse8764_query_vars' );
function wpse8764_query_vars( $query_vars )
{
    $query_vars[] = 'wpse8764_var';
    return $query_vars;
}
share|improve this answer
    
Great helped me tons :D –  Umair Feb 29 '12 at 17:57
add comment

There's a permalink endpoint feature but it's very broken when used without a trailing parameter.

For one, there is this issue, which is only fixed to the extent that they've amended the docs:

http://core.trac.wordpress.org/ticket/9476

But most importantly, the rewrite engine uses empty() at one point, detects the endpoint as empty as a result, and does mark it as set anywhere as a result. There are three workarounds.

One is what you're apparently doing at the moment, aka pass a GET parameter.

Another is to hook onto init and then adjust a global and change the REQUEST_URI accordingly when you detect your end point.

The last is to actually use a parameter, i.e. not:

/category/tasks/upcoming

but rather:

/category/tasks/filter/upcoming

share|improve this answer
    
I like the idea tinkering with the REQUEST_URI to some extend (or probably query passed into WP_Query). Never thought about that one. –  hakre Feb 9 '11 at 0:46
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.