Wordpress URL rewrite with custom $_GET variable

admin

Administrator
Staff member
I'm looking for a way to rewrite a URL similar to wordpress where:

Code:
http://www.example.com/?p=5

becomes

Code:
http://www.example.com/some-page-with-that-title

I'd like

Code:
http://www.example.com/events/?event_id=23

to become

Code:
http://www.example.com/events/some-event-title

I've discovered the wordpress rewrite api and have successfully gotten the following to rewrite the url using and events template page.

Code:
add_action( 'init', 'events_permalinks' );
function events_permalinks() {
    add_rewrite_rule(
        'events/([^/]+)/?',
        'index.php?pagename=events&event_name=$matches[1]',
        'top'
);
}

add_filter( 'query_vars', 'events_query_vars' );
function events_query_vars( $query_vars ) {
    $query_vars[] = 'event_name';
    return $query_vars;
}

And get the variable with
get_query_var( 'event_name' )

I know I need to look up the event_id in the non-wordpress events table to get the name but I'm not sure where to put the query so I can swap it into the url. Hopefully I haven't confused things. :)