Define orderby in url

is there a way to set the order of posts via the url?

/orderby/date/order/desc/

i have tried several things with add_rewrite_rule whiteout success.

add_action( 'init', 'wpse13483_init' );
function wpse13483_init()
{
    add_rewrite_rule( 'category/(.+?)/orderby/([^/]+)/order/([^/]+)(/page/?([0-9]{1,}))?/?$', 'index.php?category_name=$matches[1]&paged=$matches[5]&orderby=$matches[2]&order=$matches[3]', 'top' );
}

best, Dan.

2 Answers
2

adding this in the functions.php file works. Just remember to re-save your permalinks & empty the cache a few times to see the changes.

add_action( 'init', 'wpse13483_init' );
function wpse13483_init() {
    add_rewrite_rule( 'category/(.+?)/orderby/([^/]+)?/?$', 'index.php?category_name=$matches[1]&orderby=$matches[2]&order=asc', 'top' );
    add_rewrite_rule( 'category/(.+?)/orderby/([^/]+)/order/([^/]+)?/?$', 'index.php?category_name=$matches[1]&orderby=$matches[2]&order=$matches[3]', 'top' );
//  with pagination;
    add_rewrite_rule( 'category/(.+?)/orderby/([^/]+)/order/([^/]+)/page/([0-9]{1,})?/?$', 'index.php?category_name=$matches[1]&orderby=$matches[2]&order=$matches[3]&paged=$matches[4]', 'top' );
}

Leave a Comment