1

I have a taxonomy search form and the link output is

http://localhost/wp/?cityid=16

But i want it to be rewrited as

http://localhost/wp/cityid/16

.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>

# END WordPress

It didn't work:

RewirteRule ^wp/cityid/([0-9]+)$ ?cityid=$1 [L]

It didnt work either (from wp codex):

add_action('generate_rewrite_rules', 'geotags_add_rewrite_rules');

    function geotags_add_rewrite_rules( $wp_rewrite ) 
    {
      $new_rules = array( 
         'cityid/(.+)' => 'index.php?cityid=' .
           $wp_rewrite->preg_index(1) );

      //​ Add the new rewrite rule into the top of the global rules array
      $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }

Any help please?

1

Try this code:

function geotags_add_rewrite_rules($wp_rewrite_rules) {
    global $wp_rewrite;

    $rule_key = '%cityid%';
    $url_pattern = '([^/]+)';
    $query_string = 'cityid=';

    $wp_rewrite->add_rewrite_tag($rule_key, $url_pattern, $query_string);

    $url_structure = $wp_rewrite->root . "cityid/$rule_key/";
    $rewrite_rules = $wp_rewrite->generate_rewrite_rules($url_structure);

    $wp_rewrite_rules = $rewrite_rules + $wp_rewrite_rules;
    return $wp_rewrite_rules;
}

add_filter('rewrite_rules_array', 'geotags_add_rewrite_rules');

function geotags_add_query_var($query_vars) {
    array_push($query_vars, 'cityid');
    return $query_vars;
}

add_filter('query_vars','geotags_add_query_var');

NOTE: Remember to save permalinks so your new rule is picked up by WP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.