There is a nonhierarchical custom post type "news".


Each post has custom field "views". When someone opens post, field's value increased by one.

Would like to sort posts by this custom field, by its value.

/news/views/ = /news/?sort=views (get posts with custom field "views" and sort asc)
/news/views/page/2/ = /news/?sort=views&page=2 (similar to previous, next page)

Got working /news/?sort=views, but this link is not human-like.

It should be transformed to /news/views/ and be able to handle pagination.

How can we do this?

1 Answer 1


You need to look at add_rewrite_tag() and add_rewrite_rule()

Also read this and this topics. They are very close to you issue.

After you setup rewrite rule don't forget to flush rules

add_action( 'wp_loaded','my_flush_rules' ); // flush_rules() if our rules are not yet included
function my_flush_rules(){
    $rules = get_option( 'rewrite_rules' );
    $pattern = 'news/([^/]+)?'; // the same pattern that was used in add_rewrite_rule
    if ( !isset( $rules[$pattern] ) ) {
        global $wp_rewrite;
  • this code doesnt work Commented Nov 12, 2011 at 12:53
  • did you setup add_rewrite_tag() and add_rewrite_rule()?
    – Alexey
    Commented Nov 12, 2011 at 17:33
  • I don't know how Commented Nov 12, 2011 at 20:00

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.