WordPress.org

Ready to get started?Download WordPress

Forums

Custom Rewrite Rules - query_vars issue (1 post)

  1. merianos
    Member
    Posted 1 year ago #

    I have create the following function that is hooked on init action, in order to add my custom re-write rules:

    public function customRewriteRules()
    {
        add_rewrite_tag('%specialty%',  '([^/]+)',  'specialty=');
        add_rewrite_tag('%region%',     '([^/]+)',  'region=');
        add_rewrite_tag('%lngg%',       '([^/]+)',  'lngg=');
        add_rewrite_tag('%zpcd%',       '([^/]+)',  'zpcd=');
        add_rewrite_tag('%find%',       '([1])',    'find=');
    
        add_rewrite_rule(
            '^find/([^/]+)/([^/]+)/([^/]+)/?$',
            'index.php?find=1&specialty=$matches[1]&region=$matches[2]&s=$matches[3]',
            'top'
        );
    
        add_rewrite_rule(
            '^find/([^/]+)/([^/]+)/([^/]+)/page/([0-9]+)/?$',
            'index.php? find=1&specialty=$matches[1]&region=$matches[2]&s=$matches[3]&page=$matches[4]',
            'top'
        );
    
        add_rewrite_rule(
            '^find/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$',
            'index.php?find=1&specialty=$matches[1]&region=$matches[2]&s=$matches[3]&lngg=$matches[4]&zpcd=$matches[5]',
            'top'
        );
    
        add_rewrite_rule(
            '^find/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/page/([0-9]+)/?$',
            'index.php?find=1&specialty=$matches[1]&region=$matches[2]&s=$matches[3]&lngg=$matches[4]&zpcd=$matches[5]&page=$matches[6]',
            'top'
        );
    
        if(get_option('x_rewrite_added', 0) == 0)
        {
            update_option('x_rewrite_added', 1);
            flush_rewrite_rules();
        }
    }

    When I try the URL

    http://www.mysite.com/find/someterm/someterm/somesearch/

    I get the following query_vars

    [query_vars] => Array
    (
        [s] => none
        [region] => someterm
        [specialty] => someterm
    )

    and the following query_string

    s=none&region=someterm&specialty=someterm

    When I try the URL

    http://www.mysite.com/find/someterm/someterm/somesearch/page/4

    I get the following query_vars

    [query_vars] => Array
    (
        [s] => none
        [page] => 4
        [region] => someterm
        [specialty] => someterm
    )

    and the following query_string

    s=none&page=4&region=someterm&specialty=someterm

    But when I try to enter the following URL:

    http://www.mysite.com/find/someterm/someterm/somesearch/someterm/someterm/

    I am getting the following query_vars

    [query_vars] => Array
    (
        [s] => none
        [region] => someterm
        [specialty] => someterm
    )

    and the following query_string

    s=none&region=someterm&specialty=someterm

    as weill when I try the

    http://www.mysite.com/find/someterm/someterm/somesearch/someterm/someterm/page/2

    I am getting the following query_vars

    [query_vars] => Array
    (
        [s] => none
        [page] => 2
        [region] => someterm
        [specialty] => someterm
    )

    and the following query_string

    s=none&page=2&region=someterm&specialty=someterm

Topic Closed

This topic has been closed to new replies.

About this Topic