i have a problem with query vars and rewrite rules. I have - a page called "luoghi" - one custom field called "test_city" - a query var called "city"

Into "luoghi" page i did the query with custom fields and everything works. If i reach:


I see the list of posts filtered by custom field. I'm trying to convert ?city=milano to /milano/ with no success. These are the functions:

function myplugin_register_query_vars( $vars ) {
    $vars[] = 'city';
    return $vars;
add_filter( 'query_vars', 'myplugin_register_query_vars' );

add_action('init', 'myplugin_rewrite_tag', 10, 0);
function myplugin_rewrite_tag() {
    add_rewrite_tag( '%city%', '([^&]+)' );

add_action('init', 'myplugin_rewrite_rule', 10, 0);
function myplugin_rewrite_rule() {
    add_rewrite_rule( '^luoghi/city/([^/]*)/?', 'index.php?pagename=luoghi&city=$matches[1]','top' );

Query monitor doesn't help a lot: enter image description here

  • when you go to this URL, do you see what you excepted ? localhost/wptest/index.php?pagename=luoghi&city=milano – mmm Mar 12 '18 at 11:49
  • I see the right query. The problem is that rewrite is not working. Everything else is okay. If i reach localhost/wptest/luoghi/city/milano it works, the problem is that it's not rewriting arguments – gleenk Mar 12 '18 at 12:01
  • the rewrite works because you see "pagename=luoghi&city=milano" in the query monitor. and I try your code and it's works without modification when I go to URL http://server/luoghi/city/milano/ – mmm Mar 12 '18 at 12:08
  • yes but it doesn't rewrite pagename=luoghi&city=milano to /luoghi/city/milano automatically. That's my problem. I need to rewrite that url when i visit it – gleenk Mar 12 '18 at 12:42

Your Answer

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

Browse other questions tagged or ask your own question.