Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Created July 14, 2021 10:29
Show Gist options
  • Save jorpdesigns/e9f0e9621ed3a50181282e889a0fb565 to your computer and use it in GitHub Desktop.
Save jorpdesigns/e9f0e9621ed3a50181282e889a0fb565 to your computer and use it in GitHub Desktop.
Snippet to allow numeric URLs in WordPress
<?php
add_filter( 'wp_unique_post_slug', 'numeric_post_slug', 10, 6 );
function numeric_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
global $wpdb;
if ( ! is_numeric( $original_slug ) || $slug === $original_slug ) {
return $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
) );
if ( $post_name_check ) {
return $slug;
}
return $original_slug;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment