rewrite_rule() not preserving the query string

We have added a custom rewrite_rule to our site to allow for a pretty inbound link to be parsed and handled properly. Links are constructed as domain.com/meetings/faculty/someIDnumber

add_action( "init", "pleasing_permalinks" );
function pleasing_permalinks() {
    add_rewrite_tag( '%hash%', '([^&]+)' );
    add_rewrite_rule(
        'meetings/faculty/([^/]+)/?$',
        'index.php?p=1598&hash=$matches[1]',
        'top'
    );
    //flush_rewrite_rules();
}

Rewrite Analyzer approves of the above but when we test with actual links in the browser, the embedded ID number is not preserved. What are we missing here?

Related posts

Leave a Reply

3 comments

  1. If faculty is a child page of meetings, the rule should be:

    add_rewrite_rule(
        'meetings/faculty/([^/]+)/?$',
        'index.php?pagename=meetings/faculty&hash=$matches[1]',
        'top'
    );
    

    pagename=meetings/faculty instead of p=1598

    EDIT- or alternately:

    add_rewrite_rule(
        'meetings/faculty/([^/]+)/?$',
        'index.php?page_id=1598&hash=$matches[1]',
        'top'
    );
    

    page_id=1598 instead of p=1598

  2. Just tested your code on a clean WP installation and was able to get the variable with:

    echo $wp_query->query_vars['hash'];
    

    WordPress Codex – add_rewrite_tag:
    Note that using $_GET on a rewritten URL will not work, even if the
    rewrite includes the querystring variables. You must use $wp_query.

    Edit: As written in the correct answer ‘p’ must be replaced with ‘page_id’ to be able to keep the variables when redirecting to a post of type ‘page’.