Check this:
$categories = get_categories( 'country' );
foreach ( $categories as $category ) {
$posts_array = get_posts ( array (
'posts_per_page' => -1,
'post_type' => 'university',
'tax_query' => array (
array(
'taxonomy' => 'country',
'field' => 'term_id',
'terms' => $category->term_id,
)
)
) );
var_dump( $posts_array );
}
UPDATE:
<div class="container">
<div class="row">
<ul class="collapsible" data-collapsible="accordion">
<?php
$categories = get_terms( 'country' );
foreach ( $categories as $category ) : ?>
<li class="home-list hoverable col m6 z-depth-1">
<div class="collapsible-header">
<h5>
<i class="material-icons teal-text">add_circle_outline</i>
<?php echo 'Medical Universities in ' . esc_html( $category->name ) . ' (' . $category->count . ')'; ?>
</h5>
</div>
<div class="collapsible-body">
<?php
$posts_array = get_posts ( array (
'posts_per_page' => -1,
'post_type' => 'university',
'tax_query' => array (
array(
'taxonomy' => 'country',
'field' => 'term_id',
'terms' => $category->term_id,
)
)
) );
if ( $posts_array ) :
foreach ( $posts_array as $university ) : ?>
<h3><a href="https://wordpress.stackexchange.com/questions/301922/<?php echo esc_url( get_permalink( $university->ID ) ); ?>"></a></h3>
<?php endforeach;
else : ?>
<p>No posts found</p>
<?php endif; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>