3

With all my current Wordpress installations, whenever we try to create a post URL with just numbers, it add a “-2” on the end, like this:

example.com/002/

Becomes

example.com/002-2/

At first I thought this was due to Wordpress’s default behavior of protecting against duplicate URLs, but when I put in really long string of random numbers, I got the same behavior…

example.com/9020983498207850789287349082078930/

Became

example.com/9020983498207850789287349082078930-2/

There have been discussions of this before, but

Can I use a number for a post/page slug?

Why can posts never have a number as the link?

That last link has some suggested code to fix it, but then strongly discourages its use....

3
  • Are you sure there's no duplicates, no draft version, nothing in the trash, etc.? If you've excluded that, it might be for avoiding conflicts when your permalink structure is different (e.g. ?p=51). I'm not sure.
    – Pim
    Commented Apr 5, 2017 at 16:19
  • 1
    WordPress expects example.com/[0-9]+ to be the yearly archive (I did check it here)
    – birgire
    Commented Apr 5, 2017 at 17:30
  • @jeremyers did my solution work for you? Commented Apr 19, 2017 at 5:38

1 Answer 1

2

Hope the following code might help

add_filter( 'wp_unique_post_slug', 'mg_unique_post_slug', 10, 6 );

/**
 * Allow numeric slug
 *
 * @param string $slug          The slug returned by wp_unique_post_slug().
 * @param int    $post_ID       The post ID that the slug belongs to.
 * @param string $post_status   The status of post that the slug belongs to.
 * @param string $post_type     The post_type of the post.
 * @param int    $post_parent   Post parent ID.
 * @param string $original_slug The requested slug, may or may not be unique.
 */
function mg_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
    global $wpdb;

    // don't change non-numeric values
    if ( ! is_numeric( $original_slug ) || $slug === $original_slug ) {
        return $slug;
    }

    // Was there any conflict or was a suffix added due to the preg_match() call in wp_unique_post_slug() ?
    $post_name_check = $wpdb->get_var( $wpdb->prepare(
        "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1",
        $original_slug, $post_type, $post_ID, $post_parent
    ) );

    // There really is a conflict due to an existing page so keep the modified slug
    if ( $post_name_check ) {
        return $slug;
    }

    // Return our numeric slug
    return $original_slug;
}

The code is taken from http://magnigenie.com/how-to-achieve-numeric-urls-in-wordpress/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.