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?
Try this:
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).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 withget_query_var( 'wpse8764_var' )
.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