How to generate custom post types year archive rew

2019-09-14 17:10发布

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?

1条回答
走好不送
2楼-- · 2019-09-14 17:35

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

function px_generate_rewrite_rules($wp_rewrite)
{
    $new_rules = array(
        '([^/]+)/archive/(\d+)/?$' => 'index.php?category_name=$matches[1]&px_archive_year=$matches[2]'
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'px_generate_rewrite_rules');

Register custom query var(s)

function px_query_vars($qvars)
{
    $qvars[] = 'px_archive_year';
    return $qvars;
}
add_filter('query_vars', 'px_query_vars');

Add your custom query

function px_pre_posts($query) 
{

    if( ! is_admin() )
    {
        if( $query->is_main_query() && is_category() )
        {
            set_query_var('date_query', array(
                array(
                    array(
                        'year'  => get_query_var('px_archive_year', date('Y')),
                    )
                )
            ));
            return;
        }
    }
}
add_action('pre_get_posts', 'px_pre_posts');

Please remember that you have to flush rewrite rules if you change something. Remove this code when you're done.

add_action('init', function() {
    flush_rewrite_rules();
});

NB! Please pay attention to the title of the page. Every page should have an unique title.

查看更多
登录 后发表回答