Freddy15
Freddy15

Reputation: 31

Wordpress URL rewrite with GET variables

I have tried to search a solution but was unable to find one. In my wordpress website i've got a custom page that retrieves data from the database. Now i have this url:

http://www.domain.com/party/?title=nameoftheparty&id=4

I need to rewrite it to:

http://www.domain.com/party/nameoftheparty/4/

I tried to add the rewrite url in .htaccess but i get an 404 page.

What do i need to do?

Upvotes: 2

Views: 1537

Answers (1)

Anand Shah
Anand Shah

Reputation: 14913

Add both the code snippets to functions.php

1.We are telling WordPress that /party/nameoftheparty/4/ should be internally mapped to /party/?title=nameoftheparty&id=4

add_action( 'init', 'so27053217_init' );
function so27053217_init()
{
    add_rewrite_rule(
        '^party/([^/]*)/([^/]*)/?',
        'index.php?pagename=party&title=$matches[1]&id=$matches[2]',
        'top' );    
}

This is optional and only required if you need to make use of title and id variables in your party page. They can be accessed using get_query_var("title")

add_filter( 'query_vars', 'so27053217_query_vars' );
function so27053217_query_vars( $query_vars )
{
    $query_vars[] = 'title';
    $query_vars[] = 'id';
    return $query_vars;
}

Remember to re-save your permalinks to flush rewrite rules.

Upvotes: 3

Related Questions