I want to want to make url friendly urls for a custom function I am using in my website. I have added a custom template on my theme dir.

The url rewrite function that looks like this:

function starts_rewrite_rule() {
add_action('init', 'starts_rewrite_rule', 10, 0);

The url becomes:


All good and the parameters are read properly from the function. And when the accessed permalink changes to


the sql query changes to "ab".

After I update the Permalinks ( click save on permalinks ). I wanted to extend the functionality and add another url rewrite rule. I added a higher priority because the url patter was identical (except the new url reweire rule had an extra parameter. SO I copy paste the same function and add the extra parameter.

function x_letter_cities_starts_rewrite_rule() {
add_action('init', 'x_letter_cities_starts_rewrite_rule', 14, 0);

The url is supposed to become: site.com/7-number-cities-starting-with-pa/

But it seems like wordpresses rewrite function cant properly detect the extra parameter (7),. so the results are the same as the url that is accessed like:site.com/cities-starting-with-pa/

It seems like the priority is not working correctly. OR url rewrite rule IGNORES the first part "7-number-" and only reads the second part pattern of the url "cities-starting-with-pa/", because they end in the same pattern.

Can anyone help me in this tricky situation. I appreciate your help/clues/ideas.

Thank you, shoku i petrit.

  • Did you flush your rewrite rules already?
    – Robbert
    Apr 27, 2019 at 13:35
  • I flushed. I saved Changes on Settings> Permalinks. Apr 27, 2019 at 14:00

1 Answer 1


You are trying to match 7-number-cities-starting-with-pa URL to ^([^/]*)-letter-words-starting-with- pattern in second rule.

These rules should work:

add_action( 'init' , 'se336474_rewrite_rules', 5 );
add_filter( 'query_vars', 'se336474_query_vars' );

function se336474_rewrite_rules()
    // single pattern for URLs: 
    //    site.com/cities-starting-with-pa/      => start_letter='pa'
    //    site.com/7-cities-starting-with-pi/    => start_letter='pi', length=7
    add_rewrite_rule( '^(?:([0-9]+)-)?cities-starting-with-([^/]*)/?$', 'index.php?page_id=340&start_letter=$matches[2]&length=$matches[1]', 'top' );

    // OR for URLs:
    //    site.com/cities-starting-with-pa/         
    //    site.com/7-number-cities-starting-with-pi/
    //add_rewrite_rule( '^cities-starting-with-([^/]*)/?', 'index.php?page_id=340&start_letter=$matches[1]', 'top');
    //add_rewrite_rule( '^([0-9]+)-number-cities-starting-with-([^/]*)/?', 'index.php?page_id=340&start_letter=$matches[2]&length=$matches[1]', 'top');
function se336474_query_vars( $vars )
    $vars[] = "start_letter";
    $vars[] = "length";
    return $vars;

You can use page_id=340 only if 340 is a page type post, in other cases a redirection to canonical URL will be made.

About first rewrite rule syntax [ (?:([0-9]+)-)? ] you can read here.

  • Thank you I will try it. I appreciate your answer greatly <3 May 3, 2019 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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