0

I am passing to my url some variables to display latest posts, most viewed etc.

http://website.com/?r_sortby=highest_rated

I am wondering how to create a custom path for it instead for SEO purpose ie. http://website.com/highest-rated/

Help anyone:)

0

You could add the path via add_rewrite_endpoint, then alter the query via the pre_get_posts action. Here's a proof-of-concept example that alters the posts_per_page when domain.com/highest-rated/ is requested:

// add the endpoint
function wpa85664_add_endpoint(){
    add_rewrite_endpoint( 'highest-rated', EP_ALL );
}
add_action( 'init', 'wpa85664_add_endpoint' );


// check if the endpoint is in the current request
// and alter the query accordingly
function wpa85664_check_endpoint( $query ){
    if( array_key_exists( 'highest-rated', $query->query_vars ) )
        $query->set( 'posts_per_page', -1 );
}
add_action( 'pre_get_posts', 'wpa85664_check_endpoint' );

You could alter any relevant query vars with this method, for example a meta query to sort posts by rating.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.