$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>rewrite rules - Wordpress add_rewrite_rule with 2 variables|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

rewrite rules - Wordpress add_rewrite_rule with 2 variables

matteradmin9PV0评论

I need to create a rewrite rule that accepts 2 variables. I'm very new to this and am not sure If I'm even on the right track. If there is a better solution to what I'm trying to achieve, I'm open for suggestions.

The url should look like this: localhost/states/id/state-slug

Here is what I have:

function prefix_movie_rewrite_rule() {

add_rewrite_rule ( 'states/([A-Za-z0-9\-\_]+)', 'index.php? 
state_id=$matches[1]', 'top' );

add_rewrite_rule ( 'states/([A-Za-z0-9\-\_]+)/state([A-Za-z0-9\-\_]+)', 
'index.php?state_id=$matches[1]&state=$matches[2]', 'top' );

}

add_action( 'init', 'prefix_movie_rewrite_rule');

Using what I have, I'm able to get the first variable (state_id) but not the second (state).

I need to create a rewrite rule that accepts 2 variables. I'm very new to this and am not sure If I'm even on the right track. If there is a better solution to what I'm trying to achieve, I'm open for suggestions.

The url should look like this: localhost/states/id/state-slug

Here is what I have:

function prefix_movie_rewrite_rule() {

add_rewrite_rule ( 'states/([A-Za-z0-9\-\_]+)', 'index.php? 
state_id=$matches[1]', 'top' );

add_rewrite_rule ( 'states/([A-Za-z0-9\-\_]+)/state([A-Za-z0-9\-\_]+)', 
'index.php?state_id=$matches[1]&state=$matches[2]', 'top' );

}

add_action( 'init', 'prefix_movie_rewrite_rule');

Using what I have, I'm able to get the first variable (state_id) but not the second (state).

Share Improve this question asked Mar 14, 2019 at 18:39 Dominick AllenDominick Allen 11 bronze badge 7
  • Try with &state=state$matches[2] – Sally CJ Commented Mar 14, 2019 at 18:43
  • Not sure what you mean? – Dominick Allen Commented Mar 14, 2019 at 18:47
  • Are the post slugs in this format: state{slug}, e.g. state-one, statetwo, etc.? I.e. the slug always starts with the text state? If so, then the &state=$matches[2] in the second add_rewrite_rule() should be &state=state$matches[2] – Sally CJ Commented Mar 14, 2019 at 19:03
  • If not, then the /state([A-Za-z0-9\-\_]+) should probably be just /([A-Za-z0-9\-\_]+) so that it works with all slugs regardless the slug starts with the text "state" or not. I hope this helps you. – Sally CJ Commented Mar 14, 2019 at 19:20
  • The format i'm looking for is localhost/states/id/state-name so something like localhost/states/18/new-york – Dominick Allen Commented Mar 14, 2019 at 21:09
 |  Show 2 more comments

1 Answer 1

Reset to default 0

After lots of pulling my hair out I figured out the problem.

I needed to use the add_rewrite_tag() function to tell wordpress to allow my query vars.

function prefix_movie_rewrite_rule() {

add_rewrite_tag('%state_id%', '([A-Za-z0-9\-\_]+)');
add_rewrite_tag('%state%', '([A-Za-z0-9\-\_]+)');
add_rewrite_rule ( 'states/([A-Za-z0-9\-\_]+)/([A-Za-z0-9\-\_]+)', 'index.php? 
state_id=$matches[1]&state=$matches[2]', 'top' );

}

add_action( 'init', 'prefix_movie_rewrite_rule');
Post a comment

comment list (0)

  1. No comments so far