You can do a conditional check before your loop to see if your query has any posts. And only display the title and the div if there is any posts to display.
$your_query = new WP_Query( $args );
if( $your_query->have_posts() ) : // Add this line here to check if the query has posts.
echo '<div class="posts-group">';
echo '<h2>More blog posts</h2>';
while( $your_query->have_posts() ) : $your_query->the_post();
Also make sure to close the new IF statement after the end of your loop:
endwhile;
echo '</div><br>';
endif; // Add this line to close the IF-statement.
Update:
Exclude your current post in the Query to make this work. Otherwise the Query still contains the current post. Here is the code:
$args = array(
'post__not_in' => array( get_the_ID() ), //Add this line to exclude the current post.
'post_type' => 'blog',
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => 'blog-athlete',
'field' => 'id',
'terms' => $cat_ids
)
)
);
$relatedquery = new WP_Query( $args );
if( $relatedquery->have_posts() ) :
echo '<div class="posts-group">';
echo '<h2>More blog posts:</h2>';
while( $relatedquery->have_posts() ) : $relatedquery->the_post();
//Removed the IF here
echo '
<article class="post home-post">
<a href="' . get_permalink() . '">
<div class="post-thumbnail-img">
' . get_the_post_thumbnail($_post->ID, "small-thumbnail") . '
</div>
<h2>' . get_the_title() . '</h2>
</a>
</article>';
// Removed the closing of the IF here
endwhile;
echo '</div><br>';
endif;