• beeblesox

    (@beeblesox)


    Apparently you can’t do this:

    /cptype/postname/
    /cptype2/postname/

    When I try, it changes the slug to: /cptype2/postname-2/

    Does anyone know a workaround for this? Or will I have to find the slug creation function and do some surgery?

    EDIT: I think the only solution will be to find the code that creates the slugs. Can anyone point me to this function?

Viewing 1 replies (of 1 total)
  • Thread Starter beeblesox

    (@beeblesox)

    Ok – I figured this out.

    WordPressRoot/wp-includes/post.php, line 2793: (function wp_unique_post_slug).

    In this function, just after this line:
    } elseif ( in_array( $post_type, $hierarchical_post_types ) ) {

    there’s an sql query to make sure that the slug isn’t in use. It uses ALL hierarchical post types, instead of just the actual post type in question, which prevented using the same name even while using a separate post type.

    I changed:
    $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";

    To:
    $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = '". esc_sql($post_type) ."' AND ID != %d AND post_parent = %d LIMIT 1";

    The change is summarized as changing:
    WHERE post_type IN ( $hierarchical_post_types )
    To:
    WHERE post_type = $post_type

    So now, when its making a unique slug, it checks only against the current post type, rather than ALL the hierarchical post types. I can now have 2 posts of the same name (as long as they’re a different post type).

    Example:
    /someposttype/cars/
    /anotherposttype/cars/

    My implementation is a bit crude, however it does work ??

    How do I motion to get this included in the next update?

Viewing 1 replies (of 1 total)
  • The topic ‘CPTs – Post Slug Conflicts’ is closed to new replies.