title: Wordpress remove parent category in URL (Domain mapping)
body: <p>The following code allow me to remove parent category for my primary domain post. 
  My problem is that I am using a third party plugin in order to map a second domain in my author name.</p>
  
  <ul>
  <li>domain1.com <strong>PRIMARY DOMAIN</strong></li>
  <li>domain2.com <strong>MAPPED DOMAIN</strong></li>
  </ul>
  
  <p>Permalink structure :</p>
  
  <ul>
  <li>domain1.com/me/cat/subcat/postname => domain2.com/cat/subcat/postname</li>
  </ul>
  
  <p>So basically, domain1.com/me MAP TO domain2.com and it's <strong>Working good this way</strong></p>
  
  <p>BUT if I remove cat slug and let only subcat like this (with the script) :</p>
  
  <ul>
  <li>domain1.com/me/subcat/postname <strong>WORKING</strong> </li>
  <li>domain2.com/subcat/postname <strong>NOT WORKING</strong> (ERR_TOO_MANY_REDIRECTS)</li>
  </ul>
  
  <p><strong>Script to remove parent slug in URL</strong></p>
  
  <pre><code>add_filter( 'post_link', 'remove_parent_category', 10, 3 );
  function remove_parent_category( $permalink, $post, $leavename )
  {
      $cats = get_the_category( $post-&gt;ID );
      if ( $cats ) {
          usort( $cats, '_usort_terms_by_ID' );
          $category = $cats[0]-&gt;slug;
          if ( $parent = $cats[0]-&gt;parent ) {
              // Find parent categories and replace them in the link
              $parentcats = get_category_parents( $parent, false, '/', true );
              $permalink = str_replace( $parentcats, '', $permalink );
          }
      }
      return $permalink;
  }
  </code></pre>