$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Date-based permalinks for Custom Post Type, and custom taxonomy permalinks|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Date-based permalinks for Custom Post Type, and custom taxonomy permalinks

matteradmin9PV0评论

In my theme (a child of Twenty Sixteen) I've created a new Custom Post Type, pg_review. I've associated two custom taxonomies with it, pg_genres (hierarchical categories), and pg_authors (like tags).

Everything is working fine except permalinks... Despite trying to understand and adapt various examples I'm going round in circles.

I would like these permalinks to work:

/reading/2017/02/19/post-name/  # A single Review.
/reading/2017/02/19/            # Reviews from one day.
/reading/2017/02/               # Reviews from one month.
/reading/2017/                  # Reviews from one year
/reading/                       # The most recent Reviews (this is working).
/reading/genre/genre-name/      # Reviews in a `pg_genre`.
/reading/author/author-name/    # Reviews in a `pg_author'.

This is pretty much how conventional Posts work if given a custom permalink structure like /archive/%year%/%monthnum%/%day%/%postname%/. But how do I make this work for a Custom Post Type?

(I've tried the Custom Post Type Permalinks plugin which promises to do this, but the links for taxonomies 404 (a problem others in support seem to have too).)

In my theme (a child of Twenty Sixteen) I've created a new Custom Post Type, pg_review. I've associated two custom taxonomies with it, pg_genres (hierarchical categories), and pg_authors (like tags).

Everything is working fine except permalinks... Despite trying to understand and adapt various examples I'm going round in circles.

I would like these permalinks to work:

/reading/2017/02/19/post-name/  # A single Review.
/reading/2017/02/19/            # Reviews from one day.
/reading/2017/02/               # Reviews from one month.
/reading/2017/                  # Reviews from one year
/reading/                       # The most recent Reviews (this is working).
/reading/genre/genre-name/      # Reviews in a `pg_genre`.
/reading/author/author-name/    # Reviews in a `pg_author'.

This is pretty much how conventional Posts work if given a custom permalink structure like /archive/%year%/%monthnum%/%day%/%postname%/. But how do I make this work for a Custom Post Type?

(I've tried the Custom Post Type Permalinks plugin which promises to do this, but the links for taxonomies 404 (a problem others in support seem to have too).)

Share Improve this question edited Feb 19, 2017 at 17:12 Phil Gyford asked Feb 19, 2017 at 15:58 Phil GyfordPhil Gyford 2391 silver badge11 bronze badges 1
  • Here's an article that provides a copypasta class where you can just replace their values with your own to get your custom post working. It also explains the various parts of the code in pretty detailed manner: blog.terresquall/2021/03/… – John Doe Commented Apr 1, 2021 at 10:28
Add a comment  | 

1 Answer 1

Reset to default 5

We'll start with the taxonomies, as those are fairly simple. If you register those with simply reading/genre and reading/author as the slug argument, those should work without issue.

The post type is a bit more tricky. For that, we register with the rewrite argument set to just true. Then we add a new permastruct for that post type with:

add_permastruct(
    'pg_review',
    "/reading/%year%/%monthnum%/%day%/%pg_review%/",
    array( 'with_front' => false )
);

We'll now have the rules for single post views, but the year/month/day views will all have the wrong post type set, so we'll add some new rules to reset those with:

add_rewrite_rule(
    '^reading/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$',
    'index.php?post_type=pg_review&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
    'top'
);
add_rewrite_rule(
    '^reading/([0-9]{4})/([0-9]{1,2})/?$',
    'index.php?post_type=pg_review&year=$matches[1]&monthnum=$matches[2]',
    'top'
);
add_rewrite_rule(
    '^reading/([0-9]{4})/?$',
    'index.php?post_type=pg_review&year=$matches[1]',
    'top'
);

The last step is to filter post_type_link to insert the correct year/month/day in the permalinks:

function wpd_pg_review_permalinks( $url, $post ) {
    if ( 'pg_review' == get_post_type( $post ) ) {
        $url = str_replace( "%year%", get_the_date('Y'), $url );
        $url = str_replace( "%monthnum%", get_the_date('m'), $url );
        $url = str_replace( "%day%", get_the_date('d'), $url );
    }
    return $url;
}
add_filter( 'post_type_link', 'wpd_pg_review_permalinks', 10, 2 );
Post a comment

comment list (0)

  1. No comments so far