rewrite rules - Wordpress add_rewrite_rule with 2 variables

admin2025-06-02  1

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');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748801801a313837.html

最新回复(0)