How can I Rewrite a ‘page’ URL based on query string parameters?

I have the following page which contains a single post and a number of comments:

http://mysite.com/tips

Read More

When a user posts a comment, I add meta-data to it so I can filter comments by using these URL’s:

http://mysite/tips?id=cat&filter_id=1

http://mysite/tips?id=dog&filter_id=2

I want to use the following url’s instead:

http://mysite/tips/cat

http://mysite/tips/dog

The tips page displays a list of posts based on the query string parameters.

I worked through this tutorial, which seemed to be what I needed, but when I type in my url, I get a page not found error.
http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/

Is this possible?

Related posts

Leave a Reply

3 comments

  1. You can add a rewrite rule such as the following. Make sure to register your public query vars with WordPress so it recognizes your rewrite rule vars.

    add_rewrite_rule('^(tips)/([^/]*)/([^/]*)/?', 'index.php?name=$matches[1]&id=$matches[2]&filter_id=$matches[3]','top');
    
    add_filter('query_vars', 'foo_my_query_vars');
    function foo_my_query_vars($vars){
        $vars[] = 'id';
        $vars[] = 'filter_id';
        return $vars;
    }
    

    Visit the Permalinks Settings page to flush your permalinks.

    You can access your variables as follows:

    $id = get_query_var('id');
    $filter_id = get_query_var('filter_id');
    

    Note, you’re wanting to have two dynamic variables with one URI segment which won’t work at all. The rewrite rule above works for tips/cat/1 (1 being the filter_id).

    To test your rules, I highly recommend you to use the Monkeyman Rewrite Analyzer.

    Hope this helps you out!

  2. You can use the add_query_arg funtion:

    add_query_arg('filter_id', 1);
    add_query_arg('filter_id', 2);
    

    Now the URLs read as follows:

    http://mysite/tips/cat?filter_id=1

    http:// mysite/tips/dog?filter_id=2

    You can get the arguments’ values as follows:

    $id = get_query_var('id');
    $filter_id = get_query_var('filter_id');
    

    Here is more infomation about add_query_arg

  3. I Think you want to change the Permalink Style You can change the Permalink of your website through admin panel of WordPress go to WordPress and then in the sidebar menu go to setting and then there you will see a sub-menu of Permalink click it and you will see many Types of Permalinks provided by wordpress you can also make your custom permalink. Check out the Picture Hope it might help:

    Permalink Settings