Hide children categories from permalink.

add_filter( 'post_link', 'remove_children_category_slug', 10, 3 );
function remove_children_category_slug( $permalink, $post, $leavename ){
    $cats = get_the_category( $post->ID );
    if ( $cats ) {

        usort( $cats, '_usort_terms_by_ID' );
        foreach( $cats as $cat ) {
  
          if ( $cat->parent ) {
              $parentcategory = explode(" ",get_category_parents( $cat, false, ' ', true ));

              $parentcat = $parentcategory[0];
          } else {
       
              $parentcat = $cat->slug;
          }
        }
    }

    $permalink = home_url()."/".$parentcat."/".$post->post_name;
    
    return $permalink;
}

パーマリンク設定で子カテゴリを非表示にする方法:WordPress