Why can't I save permalink “360” for a page?

I am trying to save my page with permalink "360" but for some reason WP keeps updating the permalink to "360-2". I have checked everywhere in my WP and there is no page or post that uses permalink "360". Is 360 a permalink for something internal? If not, what is the problem?

Answers 3

  • The problem comes with using only numbers as URLs. Here is a forum thread in WP that discuss this issue. I'll cite Otto:

    WordPress 2.3 and up does not allow the post or page slugs to be all numeric. This is because that URL scheme will conflict with multi-page posts.

    There is no fix. Change them to something else.

    Alternatively, a plugin exists to allow this, if you give up on multi-page posting: http://wordpress.org/extend/plugins/allow-numeric-stubs/

    More info here: http://trac.wordpress.org/ticket/5305


  • If we check out the source of the wp_unique_post_slug() function, then we see that this is expected for hierarchical post types, other than nav_menu_item.

    If we try for example the slugs 360 or page360, then the -n slug suffix will show up.

    We can play with e.g.:

    echo wp_unique_post_slug( 
        $slug        = '360', 
        $post_id     = '', 
        $post_status = '', 
        $post_type   = 'page' 
    );
    

    or

    echo wp_unique_post_slug( 
        $slug        = 'page360', 
        $post_id     = '', 
        $post_status = '', 
        $post_type   = 'page' 
    );
    

    to see that.

    One of the "bad slug" checks, within wp_unique_post_slug(), is this one:

    preg_match( "@^($wp_rewrite->pagination_base)?\[email protected]", $slug ) 
    

    It's matched in your case:

    preg_match( "@^(page)?\[email protected]", '360' ) 
    

    hence the resulting slug suffix.

    You can also play with it here:

    https://regex101.com/r/jF3kC6/1

    Note that it's possible to modify the slug via the wp_unique_post_slug filter, but one should be really careful doing that.


  • Here's what I did if you don't like plugins for a simple job:

    // Fix issue with digits can't be slug
    add_filter('wp_unique_post_slug', 'my_allow_numeric_slug', 20, 6);
    
    function my_allow_numeric_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug)
    {
        global $wpdb;
    
        if (preg_match("@^\[email protected]", $slug) && $post_type !== 'attachment') {
            $check_sql = "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";
            $better_slug = str_replace('-2', '', $slug);
            $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $better_slug, $post_type, $post_ID, $post_parent));
    
            if (!$post_name_check) {
                return $better_slug;
            }
        }
        return $slug;
    }
    

    "@^\[email protected]" matches numeric slug with -2 postfix.

    I actually just check for -2 if it has been added cause in the other case it means that you already have e.g. 360-2 page which seems weird to me, but I suppose you got the idea.


Related Questions