$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'); ?>posts - Disable WordPress Archive Conflict Check|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)

posts - Disable WordPress Archive Conflict Check

matteradmin8PV0评论

I want to have numerical slugs in WordPress for pages but WordPress doesn't allow this by default in an attempt to preserve the slugs for use by chronological archives. The wp_unique_post_slug function includes a check which is described as "Prevent new post slugs that could result in URLs that conflict with date archives." Here's the relevant code from line 3812 from wp-includes/post.php

    // Prevent new post slugs that could result in URLs that conflict with date archives.
    $post = get_post( $post_ID );
    $conflicts_with_date_archive = false;
    if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) {
        $permastructs   = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
        $postname_index = array_search( '%postname%', $permastructs );

        /*
         * Potential date clashes are as follows:
         *
         * - Any integer in the first permastruct position could be a year.
         * - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'.
         * - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'.
         */
        if ( 0 === $postname_index ||
            ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) ||
            ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num )
        ) {
            $conflicts_with_date_archive = true;
        }
    }

Commenting out this code should in theory allow slugs like /example/2019 but WordPress continues to append -2 to slugs matching the code's regular expression. I can't find anywhere else this check is being done.

There will never be an archive under /example so the attempt at protecting users throws out functionality with no benefit.

How can this unwanted behavior be disabled?

I want to have numerical slugs in WordPress for pages but WordPress doesn't allow this by default in an attempt to preserve the slugs for use by chronological archives. The wp_unique_post_slug function includes a check which is described as "Prevent new post slugs that could result in URLs that conflict with date archives." Here's the relevant code from line 3812 from wp-includes/post.php

    // Prevent new post slugs that could result in URLs that conflict with date archives.
    $post = get_post( $post_ID );
    $conflicts_with_date_archive = false;
    if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) {
        $permastructs   = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
        $postname_index = array_search( '%postname%', $permastructs );

        /*
         * Potential date clashes are as follows:
         *
         * - Any integer in the first permastruct position could be a year.
         * - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'.
         * - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'.
         */
        if ( 0 === $postname_index ||
            ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) ||
            ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num )
        ) {
            $conflicts_with_date_archive = true;
        }
    }

Commenting out this code should in theory allow slugs like /example/2019 but WordPress continues to append -2 to slugs matching the code's regular expression. I can't find anywhere else this check is being done.

There will never be an archive under /example so the attempt at protecting users throws out functionality with no benefit.

How can this unwanted behavior be disabled?

Share Improve this question edited Feb 16, 2019 at 21:04 asked Feb 16, 2019 at 19:50 user161392user161392 1
  • Comments are not for extended discussion; this conversation has been moved to chat. – Tom J Nowell Commented Feb 16, 2019 at 20:57
Add a comment  | 

1 Answer 1

Reset to default 2

If you read a little further down past that code you are thinking about commenting out, you will see that right before the function is finished it provides a hook for the result:

return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug );

This means you can override what it is doing to your slugs:

add_filter( 'wp_unique_post_slug', 'wpse_328945_unique_post', 11, 6 );
function wpse_328945_unique_post( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ){
    return $original_slug;
}

So we are accepting 6 arguments to get the last one, original slug, and then returning the original.

Of course this will completely bypass the unique_slug functionality, and you may not want that. You could add to the function some more checking to see if it was of the type you are trying to preserve, and only return the original if so (otherwise return the $slug variable so as to do nothing).

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far