Gettind 404 not found wen click the view button for a given category custom post type portfolio

There is a conflict with the taxonomy slug and custom-post-type slug. You are keeping both same.

There are two options: 1) Either remove the rewrite => $rewrite from the taxonomy array and make it like this

$labels = array(
    'name'                       => _x( 'Portfolio Categories', 'Taxonomy General Name', 'owd' ),
    'singular_name'              => _x( 'Portfolio Category', 'Taxonomy Singular Name', 'owd' ),
    'menu_name'                  => __( 'Portfolio Category', 'owd' ),
    'all_items'                  => __( 'All Portfolio Category', 'owd' ),
    'parent_item'                => __( 'Portfolio Category', 'owd' ),
    'parent_item_colon'          => __( 'Parent Portfolio Category:', 'owd' ),
    'new_item_name'              => __( 'New Portfolio Category', 'owd' ),
    'add_new_item'               => __( 'Add Portfolio Category', 'owd' ),
    'edit_item'                  => __( 'Edit Portfolio Category', 'owd' ),
    'update_item'                => __( 'Update Portfolio Category', 'owd' ),
    'separate_items_with_commas' => __( 'Separate portfolio categories with commas', 'owd' ),
    'search_items'               => __( 'Search Portfolio Category', 'owd' ),
    'add_or_remove_items'        => __( 'Add or remove Portfolio Category', 'owd' ),
    'choose_from_most_used'      => __( 'Choose from the most used portfolio category', 'owd' ),
    'not_found'                  => __( 'Not Found Portfolio Category', 'owd' ),
);

$capabilities = array(
    'manage_terms'               => 'manage_categories',
    'edit_terms'                 => 'manage_categories',
    'delete_terms'               => 'manage_categories',
    'assign_terms'               => 'edit_posts',
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
              //Removed rewrite from here
    'capabilities'               => $capabilities,
);
register_taxonomy( 'portfolio_category', array( 'portfolio' ), $args );

or 2) rename your taxonomy slug to something else but not portfolio.

$rewrite = array(
    'slug'                       => 'my_portfolio', //Changed from portfolio to my_portfolio
    'with_front'                 => false,
    'hierarchical'               => true,
);

Also remove flush_rewrite_rules(); from your code. And after updating the code just visit settings > permalinks which will save all your settings. Why call an unnecessary function again and again whenever functions.php is called. On the other hand it’s not being called at the right place. It’s better to remove and visit settings>permalinks page whenever your edit custom post type.