I am trying to pass a variable from the url into a wordpress post.
1) I added add_query_var into functions.php, to retrieve the query string from the url
add_filter( 'init', 'add_model_more_query_var' );
function add_model_more_query_var()
{
global $wp;
$wp->add_query_var( 'model_more' );
}
2) I can retrieved the variable in my post with:
get_query_var( 'model_more', 1 );
The url structure that I am using is:
domain.com/post-type/post-title/?model_more=5
https://www.domain.com/model/benz/?model_more=5
https://www.domain.com/model/audi/?model_more=4
https://www.domain.com/model/ford/?model_more=12
Now I am trying to make the urls pretty, and rewrite them from
https://www.domain.com/model/benz/?model_more=5
To
https://www.domain.com/model/benz/model_more/5/
This is where I am stuck.
I have the following rewrite rule, but I get a 404 when I try to access the pretty url:
add_action('init','add_model_more_rewrite_rule');
function add_model_more_rewrite_rule()
{
add_rewrite_rule(
'^model_more/(\d*)$',
'index.php?model_more=$matches[1]',
'top'
);
}
^model_more
in your RegEx, where^
specifies the beginning of the string (in the case ofadd_rewrite_rule
this means the requested URL path). So, if your path is "/model/benze/model_more/5/", the^model_more
will never match since the beggingin of the math is/model/
.model/benz
a custom post type?