Ok, after trying to do it by myself as I usually do with rewrites (but this time for some reason, with no luck) I've found this answer:
Custom post type yearly/ monthly archive
But still doesn't work for me. If I log the rules, I can find the right one, it gets correctly written:
["it/press-release/([0-9]{4})/?$"]=>
string(50) "index.php?post_type=press_article&year=$matches[1]"
Just for completeness, the FIRST lines loggin the rules are (see the last one below, it's right):
array(309) {
["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$"]=>
string(87) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
string(104) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$"]=>
string(104) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/([0-9]{1,})/?$"]=>
string(105) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/?$"]=>
string(71) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
string(88) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$"]=>
string(88) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]"
["it/press-release/([0-9]{4})/([0-9]{1,2})/page/([0-9]{1,})/?$"]=>
string(89) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]"
["it/press-release/([0-9]{4})/?$"]=>
string(50) "index.php?post_type=press_article&year=$matches[1]"
But still it doesn't work. If I try to navigate to it/press-release/2016
(yes there are posts with that date) I'll get redirected to the front page.
I must say a couple of things:
- I use polylang
My custom post type is registered like this:
$args = array( 'public' => true, 'supports' => array('title','editor','page-attributes'), 'description' => 'Rassegna stampa', 'labels' => $labels, // omitting here since not relevant 'rewrite' => array('slug' => pll__('rassegna-stampa')), 'has_archive' => true, 'menu_icon' => 'dashicons-media-document' );//end args register_post_type('press_article',$args);
Can someone spot the problem?
I did something like that in my recent WorPress project where the client wanted to have year based navigation for news (posts in the category News). I used custom rewrite rules to do that.
Register custom rewrite rule
Register custom query var(s)
Add your custom query
Please remember that you have to flush rewrite rules if you change something. Remove this code when you're done.
NB! Please pay attention to the title of the page. Every page should have an unique title.