Slug adding -2 even though the other posts with the same name is in a separate post type

If you will take a look to code of wp_unique_post_slug – you will see that…

  1. If its post type – attachment it will check only ID and
    post_name
  2. If its hierarchical post type (page e.g.) – its
    will check for post_name on same level (and for sure same
    post_type)
  3. nav_menu_item also hierarchical post type but it
    doesn’t require any slug so its skipped.
  4. Else… it will check for post_name if same post_type

Please take a look to the 4 item in my list. This is your case.

WP doesn’t care if you have such slug in same post_type with a post_status as Draft , Pending or AutoDraft… but let’s say if you have such a post with post_status Published or Trash… well in this case you obliously (for me) will get dash + iterator number in your slug.

if you will run query – may be you will see a different post statuses of the posts of non Uniq slug

    SELECT COUNT( * ) AS  `total` ,  `post_name` ,  CONCAT_WS(' and ', post_status) as Statuses
    FROM  `wp_posts` 
    WHERE `post_type` = 'motogp-2013'  
    GROUP BY  CONCAT_WS(' and ', `post_name` ) `post_name` 
    ORDER BY  `total`  DESC
    LIMIT 0 , 30

Still if you not lucky to find out the resone of slug you had want to generate isn’t generating… you can use a filter for non uniq slugs…

wp_unique_post_slug it can have 7 arguments… ($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) whish you can use…

    add_filter('wp_unique_post_slug', 'se_126293_wp_unique_post_slug',10,7);
    function se_126293_wp_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug){
        // do whatever you want with a slug, but whatch out 
        return $slug;
    }

You can place this code to your theme’s functions.php or plugin…