From ca5807df8650d6dbacb96067d48668b3731fcfd7 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Wed, 29 Apr 2009 19:04:27 +0000 Subject: [PATCH] Make post slugs unique across all hierarhical post types. Props Denis-de-Bernardy. fixes #6437 git-svn-id: https://develop.svn.wordpress.org/trunk@11125 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/post.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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;