Make WordPress Core


Ignore:
Timestamp:
04/29/2009 07:04:27 PM (15 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r11109 r11125  
    17101710    global $wpdb, $wp_rewrite;
    17111711    if ( !in_array( $post_status, array( 'draft', 'pending' ) ) ) {
    1712         $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));
    1713 
    1714         if ($post_name_check || in_array($slug, $wp_rewrite->feeds) ) {
     1712        $hierarchical_post_types = apply_filters('hierarchical_post_types', array('page', 'attachment'));
     1713        if ( in_array($post_type, $hierarchical_post_types) ) {
     1714            $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";
     1715            $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID, $post_parent));
     1716        } else {
     1717            $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";
     1718            $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_type, $post_ID, $post_parent));
     1719        }
     1720
     1721        if ( $post_name_check || in_array($slug, $wp_rewrite->feeds) ) {
    17151722            $suffix = 2;
    17161723            do {
    17171724                $alt_post_name = substr($slug, 0, 200-(strlen($suffix)+1)). "-$suffix";
    1718                 $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));
     1725                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID, $post_parent));
    17191726                $suffix++;
    17201727            } while ($post_name_check);
Note: See TracChangeset for help on using the changeset viewer.