I have this WordPress pretty permalinks structure:
(where house define post_type)
I want this one url:
(that works like , and parse 2 as "nights" variable and 3 like "people" variable). Is this possible?
I have search but not found any similar question.
Thanks in advance.
I have this WordPress pretty permalinks structure:
https://example/house/name (where house define post_type)
I want this one url:
https://example/house/name/2/3 (that works like https://example/house/name, and parse 2 as "nights" variable and 3 like "people" variable). Is this possible?
I have search but not found any similar question.
Thanks in advance.
Share Improve this question asked Sep 11, 2019 at 11:18 Samuel E. CerezoSamuel E. Cerezo 134 bronze badges 3 |1 Answer
Reset to default 0The correct way to register the rewrite rule and rewrite tag for your case is:
function custom_rewrite_rules() {
add_rewrite_tag('%nights%', '([^&]+)');
add_rewrite_tag('%people%', '([^&]+)');
add_rewrite_rule('house/(.+)/(.+)/(.+)/?$', 'index.php?house=$matches[1]&nights=$matches[2]&people=$matches[3]', 'top');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
The code has been tested and works correctly.
add_rewrite_tag
in combination withadd_rewrite_rule
codex.wordpress/Rewrite_API/add_rewrite_tag – freejack Commented Sep 11, 2019 at 16:09add_rewrite_rule
unsuccessfully.add_rewrite_rule('house/([^/]+)/([^/]+)/([^/]+)/?', 'index.php?pagename=$matches[1]&nights=$matches[2]&people=$matches[3]', 'top');
. I've flushed rewrite rules, update permalinks but not working :( – Samuel E. Cerezo Commented Sep 19, 2019 at 11:26add_rewrite_rule('house/(.+)/(.+)/(.+)/?$', 'index.php?pagename=$matches[1]&nights=$matches[2]&people=$matches[3]', 'top');
. Also remember to useadd_rewrite_tag
to add the nights and people tags. – freejack Commented Sep 19, 2019 at 12:03