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
This commit is contained in:
Ryan Boren 2009-04-29 19:04:27 +00:00
parent 3e6b3326c8
commit ca5807df86
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;