How to check if there are posts with the same tag

this code is with in the loop of single.php , RIGHT?
If so then give this code the try , it should work . Problem with your code seems that you are using showpost, rather you should be using posts_per_page

Plus i see this fragments of code like

`$test = ""; 

$test .= ',' . $tag->name; 

$test = substr($test, 1);`

I don’t seem to find its use.

And then i see this if($post->ID != $idPost)

$idPost seems to be the id of the single post , whose tags you get initially , right?

Then this is how i will try to solve this problem.

  <?php
$posttags = get_the_tags();
if ($posttags) :
?>
    <div class="otrasObras">
      <h3>Contenido relacionado:</h3>
      <ul>
<?php    foreach($posttags as $tag) :        
        $my_args= array('tag_id'=>$tag->term_id,
                        'posts_per_page'=>2,   
                        );
        $myloop= new WP_Query($my_args); 

        if($myloop->have_posts()): 

            if($myloop->have_posts()):
?>

                    <?php
                    while ($myloop->have_posts()) : $myloop->the_post();                    
                    if(($myloop->post->ID) == $idPost)continue;
                    ?>

                    <li>
                        <a href="https://wordpress.stackexchange.com/questions/194910/<?php the_permalink(); ?>"  title="Ver la obra <?php the_title(); ?>">
                            <h4><?php the_title(); ?></h4>
                        </a>
                    </li>
                    <?php
                    break;                    
                    endwhile;
                    wp_reset_query();
                    ?>

<?php
            endif;
        endif;
    endforeach;
?>
     </ul>
            </div>    
<?php
endif;
?>