I found solution!
A Monkeyman Rewrite Analyzur plugin was very helpful: https://wordpress.org/plugins/monkeyman-rewrite-analyzer/
So now, I have such working urls…
single.php:
/magazine-name/issue-year/issue/article-name
archive.php:
/magazine-name/issue-year/issue
/magazine-name/issue-year
/magazine-name
On a custom post editor page in wp-admin I use Advanced Custom Fields plugin for issue year and issue. You can also define meta fields by yourself.
Next I added rewrite tags for issue year and issue:
function custom_rewrite_tag() {
add_rewrite_tag('%issue_year%', '([0-9]{4})' );
add_rewrite_tag('%issue%', '([0-9]+)' );
}
add_action('init', 'custom_rewrite_tag');
Next, I added rewrite rewrite rules for any combination of urls:
function custom_rewrite_rule() {
// URL: /magazine/year/issue/title
add_rewrite_rule('^([^/]+)/([0-9]{4})/([0-9])/(.?.+?)?(:/([0-9]+))?/?$', 'index.php?post_type=$matches[1]&issue_year=$matches[2]&issue=$matches[3]&name=$matches[4]', 'top');
// URL: /magazine/year/issue
add_rewrite_rule('^([^/]+)/([0-9]{4})/([0-9])?$', 'index.php?post_type=$matches[1]&issue_year=$matches[2]&issue=$matches[3]', 'top');
// URL: /magazine/year
add_rewrite_rule('^([^/]+)/([0-9]{4})?$', 'index.php?post_type=$matches[1]&issue_year=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_rule');
At the end I replace standard urls for my own, with issue year and issue
function custom_permalink($url, $post) {
if ($post->post_type == 'magazine-name-1' || $post->post_type == 'magazine-name-2' || $post->post_type == 'magazine-name-2' ) {
global $post;
$post_type = $post->post_type;
$issue_year = get_field('issue_year', $post->ID); // ACF; for meta: get_post_meta($post->ID, '$issue', true);
$issue = get_field('issue', $post->ID); // ACF
$url = str_replace( $post_type . "https://wordpress.stackexchange.com/", $post_type . "https://wordpress.stackexchange.com/" . $issue_year . "https://wordpress.stackexchange.com/" . $issue . "https://wordpress.stackexchange.com/" , $url);
}
return $url;
}
add_filter('post_type_link', 'custom_permalink', 10, 2);