WordPress Duplicate Slugs for Different Post Types

Written by | July 8, 2013 | Posted in Process

Today I stumbled across the issue of needing to use the same slugs across 3 different post types. For example, I need the following page URLS:

example.com/chapel-hill (Page)
example.com/restaurant/chapel-hill (Restaurant Post Type)
example.com/cooking-school/chapel-hill (Cooking School Post Type)

Because WordPress does not allow posts in hierarchical post types to share the same slug, even if they are in different post types, it kept appending numbers to the end of the slugs when I saved the page. I found this ticket that offered a patch for the wp-includes/post.php file. Since I am hesitant to ever edit core files, I came up with the following function to include in my theme’s functions.php file. It could very easily be modified to be a plugin, but hopefully WP will include this fix in future releases soon.

function wp_cpt_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug) {
if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
return $slug;

global $wpdb, $wp_rewrite;

// store slug made by original function
$wp_slug = $slug;

// reset slug to original slug
$slug = $original_slug;

$feeds = $wp_rewrite->feeds;
if ( ! is_array( $feeds ) )
$feeds = array();

$hierarchical_post_types = get_post_types( array('hierarchical' => true) );
if ( 'attachment' == $post_type ) {
// Attachment slugs must be unique across all types.
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );

if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
$suffix = 2;
do {
$alt_post_name = substr ($slug, 0, (200 - ( strlen( $suffix ) + 1 ) )) . "-$suffix";
$post_name_check = $wpdb->get_var( $wpdb->prepare($check_sql, $alt_post_name, $post_ID ) );
$suffix++;
} while ( $post_name_check );
$slug = $alt_post_name;
}
} elseif ( in_array( $post_type, $hierarchical_post_types ) ) {
if ( 'nav_menu_item' == $post_type )
return $slug;
// Page slugs must be unique within their own trees. Pages are in a separate
// namespace than posts so page slugs are allowed to overlap post slugs.
$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, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
$suffix = 2;
do {
$alt_post_name = substr( $slug, 0, (200 - ( strlen( $suffix ) + 1 ) )) . "-$suffix";
$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;
}
} else {
// Post slugs must be unique across all posts.
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );

if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
$suffix = 2;
do {
$alt_post_name = substr( $slug, 0, (200 - ( strlen( $suffix ) + 1 ) )) . "-$suffix";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
$suffix++;
} while ( $post_name_check );
$slug = $alt_post_name;
}
}

return $slug;
}
add_filter('wp_unique_post_slug', 'wp_cpt_unique_post_slug', 10, 6);