Viewing 1 replies (of 1 total)
  • Before we dive into that, it’s important to know that when WordPress receives a URL like this:

    example.org/2014/12/hello-world/

    There is a rewrite rule:

    [([0-9]{4})/([0-9]{1,2})/([^/]+)(/[0-9]+)?/?$] => index.php?year=$matches[1]&monthnum=$matches[2]&name=$matches[3]&page=$matches[4]

    That will convert the permalink to:

    example.org/?year=2014&monthnum=12&name=hello-world$page=

    Which is the basis of the database query:

    SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND (
      ( YEAR( wp_posts.post_date ) = 2014 AND MONTH( wp_posts.post_date ) = 12 )
    ) AND wp_posts.post_name = 'hello-world' AND wp_posts.post_type = 'post'  ORDER BY wp_posts.post_date DESC

    WordPress also has built-in rules for URLs like:

    example.org/2014
    example.org/2014/12
    example.org/2014/12/hello-world/comments

    When changing something this ingrained, all those aspects would need to be accounted for when converting the default four-digit year to a two-digit year. With that in mind, my recommendation would be to keep it as-is.

    That said, you posted this in the hacks forum, so you’re probably prepared that this might get messy. Read on.

    Regardless of how you do this, you’ll need the post_link filter to output the URL in a format of your choice.

    The next step is ensuring that WordPress can still find your post.

    If all your existing and future posts are between the year 2000 and 2999, you could prepend the “20” prior to making the query via pre_get_posts.

    You could also add new rewrite rules using the add_rewrite_rule function that prepends the “20” prior to making a query.

    If you don’t want to hardcode the “20,” you could strip out the year all together using pre_get_posts and rely on WordPress to find your post with one less parameter (just month and post name).

    This should give you a few ideas and a starting point.

Viewing 1 replies (of 1 total)

The topic ‘Permalink and Year’ is closed to new replies.