0

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'
    );
}
3
  • One issue I see is that you're using ^model_more in your RegEx, where ^ specifies the beginning of the string (in the case of add_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/.
    – phatskat
    Jan 26, 2018 at 15:09
  • Is model/benz a custom post type? Jan 26, 2018 at 15:12
  • model = custom post type and benz = post title
    – Philip
    Jan 26, 2018 at 15:36

1 Answer 1

0

There are a couple of issues with your regular expression:

^model_more/(\d*)$
^--- Says "match start of line"

In your URLs, the start of the line is actually /model/, so ^model_more will never match. Instead, try a regex like this:

^model/[^/]+/model_more/(\d+)/?$
  • [^/]+ says "match anything except /, since I would guess there are multiple models under /model/.
  • (\d+) ensures that only digits are captured - (\d*) would capture anything after the number.
  • /?$ says "the end of the line might be preceded by a slash, but not necessarily.

Edit: You can see the regex in action at https://regex101.com/r/OhOFTD/1

4
  • I dont get the 404 anymore :), but wordpress is directing the url to template archive.php, and not to single.php. domain.com/model/benz/?model_more=5 -> single-model.php and domain.com/model/benz/model_more/5 -> archive.php
    – Philip
    Jan 26, 2018 at 15:46
  • If you go to /index.php?model_more=5 do you get the page you're expecting?
    – phatskat
    Jan 26, 2018 at 15:52
  • no, i have to go to: /?model=benz&model_more=5 "model" is the custom post type, "Benz" is the post slug, and "model_more" the query var.
    – Philip
    Jan 26, 2018 at 16:27
  • Gotcha, so try this: ^model/([^/]+)/model_more/(\d+)/?$ and then for the second parameter: index.php?model=$matches[1]&model_more=$matches[2]
    – phatskat
    Jan 26, 2018 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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