0

I'm stuck on rewriting my url. been searching for days hacking code together but to no avail - So looking for help now.

My url is:

https://domainname.com/topic/ux-process/?type=newsletter

I want the URL to be:

https://domainname.com/topic/ux-process/type/newsletter

("topic" is a custom taxonomy)

This post seemed the most concise but couldn't get it to work

https://code.tutsplus.com/articles/custom-page-template-page-based-on-url-rewrite--wp-30564

Help getting this working would make my day.

0

You can use a rewrite endpoint for this.

The first step is to give your custom taxonomy an endpoint mask when you register the taxonomy:

$args = [
    'rewrite' => [
        'slug' => 'topic',
        'ep_mask' => EP_CATEGORIES
    ],
    // the rest of your args...
];
register_taxonomy( 'topic', array( 'post' ), $args );

The next step is to add the endpoint. This should be hooked to run on init, just like your taxonomy registration code.

add_rewrite_endpoint( 'type', EP_CATEGORIES );

Don't forget to flush rewrite rules after adding this code. The easiest way to do this is by visiting the Settings > Permalinks page.

You should now be able to add type/newsletter onto the end of your taxonomy links. To access the passed value, use get_query_var:

$type = get_query_var( 'type', false );

If the code currently expects to find type in $_GET['type'] and you can't change that, you can manually set that value before the code that tries to access it:

$_GET['type'] = get_query_var( 'type', false );

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.