Make post slugs unique across all hierarhical post types. Props Denis-de-Bernardy. fixes #6437

git-svn-id: http://svn.automattic.com/wordpress/trunk@11125 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-04-29 19:04:27 +00:00
parent 1d2a1111a0
commit b622d954e3
1 changed files with 10 additions and 3 deletions

View File

@ -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;