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:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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)?<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a3ece387">[email protected]</a>", $slug )
It’s matched in your case:
preg_match( "@^(page)?<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="593d727d19">[email protected]</a>", '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.
Method 3
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("@^<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a1e5157485e3a">[email protected]</a>", $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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0