diff --git a/wp-includes/post.php b/wp-includes/post.php index 94d3b69a40..8482405958 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -1709,13 +1709,20 @@ function check_and_publish_future_post($post_id) { function wp_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent) { global $wpdb, $wp_rewrite; if ( !in_array( $post_status, array( 'draft', 'pending' ) ) ) { - $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $slug, $post_type, $post_ID, $post_parent)); + $hierarchical_post_types = apply_filters('hierarchical_post_types', array('page', 'attachment')); + if ( in_array($post_type, $hierarchical_post_types) ) { + $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode("', '", $wpdb->escape($hierarchical_post_types)) . "' ) AND ID != %d AND post_parent = %d LIMIT 1"; + $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID, $post_parent)); + } else { + $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1"; + $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_type, $post_ID, $post_parent)); + } - if ($post_name_check || in_array($slug, $wp_rewrite->feeds) ) { + if ( $post_name_check || in_array($slug, $wp_rewrite->feeds) ) { $suffix = 2; do { $alt_post_name = substr($slug, 0, 200-(strlen($suffix)+1)). "-$suffix"; - $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $alt_post_name, $post_type, $post_ID, $post_parent)); + $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID, $post_parent)); $suffix++; } while ($post_name_check); $slug = $alt_post_name;