WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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

I created a custom page template movie-page.php and a page movie in WP. The template shows fine when I visit site.com/movie/

I want to add movie_id as a query var. Here's my code:

add_filter('query_vars', 'poe_query_vars');
function poe_query_vars( $vars ) {
    $vars[] = 'movie_id';
    return $vars;

I want to add rewrite rules so that site.com/movie/123/ will give me movie_id=123. I'm not concerned with page numbers, although I could just use the page query var, I need to learn how to do this right.

add_filter('rewrite_rules_array', 'poe_rewrite_rules');
function poe_rewrite_rules($rules) {
    $custom = array('movie/([0-9]+)/?$' => 'index.php?pagename=movie&movie_id=$matches[1]');
    return $custom + $rules;

I have clicked on Save Permalinks, and can verify that the rule exists. The rule is at the top of the rules, and lower down I see the others created by the custom page template. In my movie-page.php template, I am echoing the rewrite rules, and the query vars and I can't get /movie/123/ to give me a movie_id => 123.

Edit - adding the rewrite rules related to movie for reference

'/movie/([0-9]+)/([0-9]+)/?$' => 'index.php?pagename=movie&movie_id=$1&page=$2',
'/movie/([0-9]+)/?$' => 'index.php?pagename=movie&movie_id=$1',
'movie/attachment/([^/]+)/?$' => 'index.php?attachment=$1',
'movie/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$1&tb=1',
'movie/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$1&feed=$2',
'movie/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$1&feed=$2',
'movie/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$1&cpage=$2',
'(movie)/trackback/?$' => 'index.php?pagename=$1&tb=1',
'(movie)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?pagename=$1&feed=$2',
'(movie)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?pagename=$1&feed=$2',
'(movie)/page/?([0-9]{1,})/?$' => 'index.php?pagename=$1&paged=$2',
'(movie)/comment-page-([0-9]{1,})/?$' => 'index.php?pagename=$1&cpage=$2',
'(movie)(/[0-9]+)?/?$' => 'index.php?pagename=$1&page=$2',
share|improve this question
up vote 1 down vote accepted

When creating an internal rewrite rule, $1 should instead be $matches[1].

share|improve this answer
I've tried both, $1 and $matches[1] with the exact same outcome. It didn't work. I'll leave it at $matches[1] for now. – Poe Feb 11 '13 at 18:03
using your code and changing movie_id=$1 to movie_id=$matches[1] works for me in twenty eleven theme, movie_id is accessible via get_query_var('movie_id') – Milo Feb 11 '13 at 18:14
also, a slightly cleaner method is using add_rewrite_rule hooked to init rather than manipulating the rules array directly. – Milo Feb 11 '13 at 18:16
thanks. did you create a custom template, and a page named movie in WP? – Poe Feb 11 '13 at 18:16
yes, except I created a page-movie.php template rather than creating a custom template and assigning it, works either way though. – Milo Feb 11 '13 at 18:18

Your Answer


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

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