WordPress Url re-writing

So I have a WordPress page here:

http://12.ourgreenville.com/real_estate/

Read More

I want to be able to place two variable (city/state) in the URL, then work
with them. A URL would look like this:

http://12.ourgreenville.com/real_estate/1/2/

Please let me know what is needed. I placed your code in functions.php as
follows:

function add_rewrite_rules( $wp_rewrite )
{
    $new_rules = array(
                     '('.$template_page_name.')/real_estate/(.*?)/?([0-9]{1,})/?$' =>'index.php?p=5351city='.$wp_rewrite->preg_index(1).'&state='.$wp_rewrite->preg_index(2)
                 );

 $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');

function query_vars($public_query_vars) {

    $public_query_vars[] = "city";
    $public_query_vars[] = "state";
    return $public_query_vars;
}
add_filter('query_vars', 'query_vars');

I don’t know what i am doing wrong.kindly help me.

Related posts

Leave a Reply

1 comment

  1. There is one very important step you missed – to flush the rules in order to activate your changes on them. Here it is my working variant with adaption for your case:

    add_action('init', 'prefix_attach_page_rules');
    add_filter('query_vars', 'prefix_attach_page_query_vars');
    add_action('parse_request', 'prefix_attach_page_request');
    
    function prefix_attach_page_rules()
    {
        add_rewrite_rule('^' . urlencode(get_option('non_latin_data')) . '/(d+)/(d+)/?$', 'index.php?prefix_view=my_special_id&prefix_city_=$matches[1]&prefix_state=$matches[2]', 'top');
    
    
        flush_rewrite_rules(); // IMPORTANT
    }
    
    function prefix_attach_page_query_vars($query_vars)
    {   
        $query_vars[] = 'prefix_city';
        $query_vars[] = 'prefix_state';  
        return $query_vars;
    }
    
    function prefix_attach_page_request(&$wp)
    {
        // If the page request is really from our rewrite url
        if(!array_key_exists('prefix_view', $wp->query_vars))
            return;
    
        $city= $wp->query_vars['prefix_city'];
        $state = $wp->query_vars['prefix_state'];
    
        // we are on the rewrite rule
        exit; // in case very custom page like include
    }
    

    Prefix, in case of procedural implementation as yours is very important. You can place anything you like, but it allows you to escape situations of duplicated common names.

    It is not exactly necessary to use the selected actions or filters you should be able to use yours, but important is to make your rule with higher priority (top) and to flush the rules.

    prefix_attach_page_rules will tell WordPress for our rules

    prefix_attach_page_request will do our job when parsing_request in this case by the given query vars.

    There is another StackOverflow: WordPress Answers – dedicated on WordPress, you could find many solutions there, also.

    Additional (for the regex based on the comment link):

    add_rewrite_rule('^real_estate/(d+)/(d+)/?$', 'index.php?prefix_view=real_estate&real_estate_city_id=$matches[1]&real_estate_state_id=$matches[2]', 'top');
    

    Then you should search them real_estate_city_id and real_estate_state_id in query vars and page_request…