1

I'm struggling a bit with the permalinks on yearly archives for a custom post type.

I'm using wp_get_archives to get a list of the yearly archives but the url for these comes out as follows:

http://www.example.localhost/2021/?post_type=reports

I would like the urls to match this pattern:

http://www.example.localhost/reports/2021/

I followed the example in this answer but the urls are still outputed as in my first example.

public function urlRewrites(){
    $slug='reports;
    add_rewrite_rule(
        $slug.'/([0-9]{4})/?$',
        'index.php?post_type='.$slug.'&year=$matches[1]',
        'top'
    );
}

I also tried the solution mentioned here. This one changes the urls I get from wp_get_archives but they return 404s and has a trailing querystring.

http://www.example.localhost/reports/2020/?post_type=reports

Example code:

public function urlRewritesAgain(){
    global $wp_rewrite;
    $wp_rewrite->date_structure = 'reports/%year%/%monthnum%/%day%';
}

I am resaving my permalinks each time and I am hooking on to init. Am I doing something massively wrong here? Is there a better solution I should be using?

2

0

Your Answer

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