diff --git a/wp-admin/post.php b/wp-admin/post.php index 315b5e4281..0f63d87505 100644 --- a/wp-admin/post.php +++ b/wp-admin/post.php @@ -96,15 +96,30 @@ case 'post': if ('' != $_POST['advanced']) $post_status = 'draft'; if ('' != $_POST['savepage']) $post_status = 'static'; + + $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->posts'"); $post_ID = $id_result->Auto_increment; if ( empty($post_name) ) { - $post_name = sanitize_title($post_title, $post_ID); + $post_name = sanitize_title($post_title, $post_ID); } else { $post_name = sanitize_title($post_name, $post_ID); } + if ('publish' == $post_status) { + $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1"); + if ($post_name_check) { + $suffix = 2; + while ($post_name_check) { + $alt_post_name = $post_name . "-$suffix"; + $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1"); + $suffix++; + } + $post_name = $alt_post_name; + } + } + $postquery ="INSERT INTO $wpdb->posts (ID, post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, post_modified, post_modified_gmt, post_parent, menu_order) VALUES @@ -281,12 +296,6 @@ case 'editpost': $post_parent = $_POST['parent_id']; } - if (empty($post_name)) { - $post_name = sanitize_title($post_title, $post_ID); - } else { - $post_name = sanitize_title($post_name, $post_ID); - } - $trackback = $_POST['trackback_url']; // Format trackbacks $trackback = preg_replace('|\s+|', '\n', $trackback); @@ -296,6 +305,25 @@ case 'editpost': if ( 'publish' == $post_status && (!user_can_create_post($user_ID)) && 2 != get_option('new_users_can_blog') ) $post_status = 'draft'; + if (empty($post_name)) { + $post_name = sanitize_title($post_title, $post_ID); + } else { + $post_name = sanitize_title($post_name, $post_ID); + } + + if ('publish' == $post_status) { + $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1"); + if ($post_name_check) { + $suffix = 2; + while ($post_name_check) { + $alt_post_name = $post_name . "-$suffix"; + $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1"); + $suffix++; + } + $post_name = $alt_post_name; + } + } + if (user_can_edit_post_date($user_ID, $post_ID) && (!empty($_POST['edit_date']))) { $aa = $_POST['aa']; $mm = $_POST['mm']; diff --git a/wp-includes/functions-post.php b/wp-includes/functions-post.php index 0b6ba30ba2..937015f223 100644 --- a/wp-includes/functions-post.php +++ b/wp-includes/functions-post.php @@ -36,7 +36,20 @@ function wp_insert_post($postarr = array()) { $ping_status = get_settings('default_ping_status'); if ( empty($post_parent) ) $post_parent = 0; - + + if ('publish' == $post_status) { + $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1"); + if ($post_name_check) { + $suffix = 2; + while ($post_name_check) { + $alt_post_name = $post_name . "-$suffix"; + $post_name_check = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name = '$alt_post_name' AND post_status = 'publish' AND ID != '$post_ID' LIMIT 1"); + $suffix++; + } + $post_name = $alt_post_name; + } + } + $sql = "INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_modified, post_modified_gmt, post_content, post_title, post_excerpt, post_category, post_status, post_name, comment_status, ping_status, post_parent) VALUES ('$post_author', '$post_date', '$post_date_gmt', '$post_date', '$post_date_gmt', '$post_content', '$post_title', '$post_excerpt', '$post_cat', '$post_status', '$post_name', '$comment_status', '$ping_status', '$post_parent')";