3

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?

3 Answers 3

4

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');
5
  • I'm getting a "Fatal error: Unsupported operand types" error for the line that starts off with $wp_rewrite->rules. Any ideas?
    – user1462
    Commented Feb 7, 2011 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
    Commented Feb 7, 2011 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
    Commented Feb 7, 2011 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
    Commented Feb 7, 2011 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
    Commented Feb 7, 2011 at 17:29
1

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;
}
1
  • Great helped me tons :D Commented Feb 29, 2012 at 17:57
1

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

1
  • 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
    Commented Feb 9, 2011 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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