0

I'm trying to do a rewrite, so that the query var is going to look pretty:

This is the URL structure I got now:

example.com/post-type-archive/post-name/?subpage=about

This is what I want:

example.com/post-type-archive/post-name/about

Please note, that post_name is changing depending what site you're on. All the answers I've found so far, didn't consider that.

This is my current code:

add_filter( 'query_vars', function($vars) {
    $vars[] = 'subpage';
    return $vars;
});
add_action('init', function() {
    add_rewrite_tag('%subpage%', '([^/]+)');
    add_rewrite_rule('[^post-type-archive/]([^/]+)','index.php?name=$matches[1]&subpage=$matches[2]','top');
});

This makes the page return an 404 - page not found page.

And yes... I have flushed the rewrite rules after every changes I've made.


EDIT

I got it to work by using this:

add_action( 'init', function() {
    add_rewrite_tag('%subpage', '([^&]+)');
    add_rewrite_rule(
        '^post-type-archive/([^/]*)/([^/]*)/?',
        'index.php?post_type=post-type-archive&name=$matches[1]&subpage=$matches[2]',
        'top'
    );
});

The only problem now, is that pagination don't work anymore on post-type-archive.

  • How are you using subpage in your code? If you rewrite the URL like this you need to use get_query_var( 'subpage' ). $_GET['subpage'] won't work anymore. – Jacob Peattie Feb 27 at 14:19
  • Sorry. Forgot to tell that my code, returns an 404 - page not found page – Jesper Feb 27 at 14:26
0

So, first up, I don't believe that you need a rewrite tag in this situation, so you can omit that.

The issue, I believe, with the remaining code is that your rewrite rule is only capturing 1 match (because there's only one set of ()), so subpage isn't receiving a value. Additionally, your rule seems to exclude any posts of your post type ( because of the ^). Actually, it seems to exclude any URLs where the post name begins with any of the letters p,o,s,t,_,t,y,p,e.

I believe your rewrite rule should be:

add_rewrite_rule( 'post_type/([^/]+)/([^/]+)/?', 'index.php?name=$matches[1]&subpage=$matches[2]', 'top' );

Then you just need to make sure that in your template you're accessing the subpage value correctly. $_GET['subpage'] will no longer work. Instead you need to use get_query_var( 'subpage' ).

Also make sure you re-save your Permalinks settings.

  • Hi Jacob. Thanks for your answer. After trying your code, I no longer get a 404 error page, but instead it redirects me straight to the post_name/ page – Jesper Feb 27 at 14:49
  • What does subpage do, by the way? What is the subpage? Something you do in your template? – Jacob Peattie Feb 27 at 14:51
  • On the division-page, there is a submenu: About, Latest news, Contact. This menu-structure is the same on all division-pages, but the content within each submenu-pages are different for each individual division – Jesper Feb 27 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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