rewrite rules and querystring

You can add your own rewrite rule which will let you tweak the query via URL parameters:

add_action( 'init', 'rewrite_photo_url' );
function rewrite_photo_url() {
    add_rewrite_rule( 'photos/([^/]+)/?$','index.php?page=photos&photo_id=$matches[1]', 'top' );
}

If you need to use a custom variable, i.e. ‘photo_id’, you have to register the variable so it will be recognized by the query:

add_filter( 'query_vars', 'register_custom_query_vars', 1 );
function register_custom_query_vars( $vars ) {
    array_push( $vars, 'photo_id' );
    return $vars;
}

Leave a Comment