WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The code I'm using to rewrite urls with query vars:

function add_query_vars($aVars) {
    $aVars[] = "city";
    $aVars[] = "address";
    $aVars[] = "pg";
    return $aVars;
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');

function add_rewrite_rules($aRules) {
    $aNewRules = array( 'listing/([a-zA-Z\-0-9]+)/([a-zA-Z\-0-9]+)$' => 'index.phppagename=listing&city=$matches[1]&address=$matches[2]',
    '([a-zA-Z\-_]+)/pg/([0-9]+)$' => 'index.php?pagename=matches[1]&pg=$matches[2]'); 

    $aRules = $aNewRules + $aRules;

    return $aRules;
// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules');

seems to work fine,

but I get a 404 error as soon as I put wordpress-page/pg/3 in the url

I'm using the standard "pretty permalinks" option in wp settings.

Can the wp query var: "pagename" be variable? Or is there something else wrong with my code?

Thanks in advance!

share|improve this question
It's a missing '$' before matches[1]. I'll post the answer as soon as my reputation allows me. – Eric Harrison Jul 26 '12 at 16:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.