Define orderby in url

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

/orderby/date/order/desc/

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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' );
    }
    
  2. Creating URL rewrite rules is something entirely different from using URL query variables to sort the Loop query.

    I would suggest tackling the latter, before worrying about the former.

    First, you need to register your URL query variables, e.g. (in functions.php):

    function mytheme_loop_sort_queryvars( $qvars ) {
        $qvars[] = 'loop_orderby';
        $qvars[] = 'loop_order';
        return $qvars;
    }
    add_filter( 'query_vars', 'mytheme_loop_sort_queryvars' );
    

    So, now that you’ve registered your query vars, use them to modify the default Loop query.

    First, prepare the query variables, e.g. (in the appropriate template files):

    <?php
    
    // Orderby Query Filter
    $loop_orderby = '';
    // Array of valid 'orderby' parameters, for validating the URL query string
    $valid_loop_orderbys = array( 'ID', 'author', 'title', 'date', 'modified', 'parent', 'rand', 'comment_count', 'menu_order' ); //note: excluding 'meta_value', for simplicity
    // Do something only if the query var is set
    if ( isset( $wp_query->query_vars['loop_orderby'] ) ) {
        // Make sure that only a valid 'orderby' parameter is used
        // If URL string value isn't valid, use default 'date'
        $title_filter = ( in_array( $wp_query->query_vars['loop_orderby'], $valid_loop_orderbys ) ? $wp_query->query_vars['loop_orderby'] : 'date' );
    }
    // Order Query Filter
    $loop_order = '';
    // Array of valid 'order' paramters, for validating the URL query string
    $valid_loop_orders = array( 'ASC', 'DESC' );
    // Do something only if the query var is set
    if ( isset( $wp_query->query_vars['loop_order'] ) ) {
        // Make sure that only a valid 'order' parameter is used
        // If URL string value isn't valid, use default 'DESC'
        $title_filter = ( in_array( $wp_query->query_vars['loop_order'], $valid_loop_orders ) ? $wp_query->query_vars['loop_order'] : 'DESC' );
    }
    ?>
    

    Now, use the query variables to modify the loop. First, set up your custom argument array:

    <?php
    $custom_orderby_query_args = array();
    
    if ( '' != $filter_loop_orderby ) {
        $custom_orderby_query_args['orderby'] = $filter_loop_orderby;
    }
    if ( '' != $filter_loop_order ) {
        $custom_orderby_query_args['order'] = $filter_loop_order;
    }
    ?>
    

    Now, merge your custom argument array with the default loop query args:

    <?php
    // Globalize the default query
    global $wp_query;
    // Merge arguments
    $customized_query_args = array_merge( $wp_query->query, $custom_orderby_query_args );
    // Modify the query output
    query_posts( $customized_query_args );
    ?>
    

    Note: you’ll have to determine exactly how you’re applying the URL query strings. This code just uses the query strings once they’re in place.