[wp-hackers] Custom permalinks with the Rewrite API

Austin Matzko if.website at gmail.com
Fri Oct 9 16:15:48 UTC 2009

On Fri, Oct 9, 2009 at 10:48 AM, Eric Martin <emartin24 at gmail.com> wrote:
> I want to be able to allow users to custom sort a group of posts by
> adding a custom rewrite rule. I'm thinking of the following structure:
> /some-slug/sort/title:desc/
> /some-slug/sort/date:desc/
> /some-slug/sort/downloads:desc/
> I have tried doing this a couple of ways, but can't seem to get it to
> work:
> - some-slug as a page
> - some-slug as a category and using permalink structure of /%category%/
> %postname%/
> One catch is that the sorting should still work with pagination.
> Depending on which they do first, the URI could look different:
> /some-slug/sort/title:desc/page/2
> /some-slug/page/2/sort/title:desc/
> I tried using an endpoint first, but that doesn't seem to work with 2
> endpoints together, which makes sense.
> So, I'm curious if others have done this and if there are any
> suggestions on the "best" approach. I'd love to see some examples and
> appreciate any help you can provide.

If "some-slug" is for pages, why don't you attach a callback to the
"page_rewrite_rules" filter ("post_rewrite_rules" for posts)?  Then
copy the array of rules passed to the callback, and loop through that
copy, modifying each key and value to work with your rewrite scheme.
Then prepend the modified array to the current ones and return.

Or create the modified version by temporarily modifying
$wp_rewrite->page_structure, then calling
$wp_rewrite->page_rewrite_rules() to generate the modified version.
However, I don't think that would help with something like

> /some-slug/page/2/sort/title:desc/

More information about the wp-hackers mailing list