At the bottom of each post, I want to display posts that have the same tag. Based on numerous examples of function doing something similar, I created the function below. I call it from with in the loop from the custom post php that I created. It's called succesfully, but I don't get the list of titles that I expect to get. How do I solve this?
function show_related(){
$posttags = get_the_tags();
if ($posttags) {
$ids = array();
$relatedpostids = array();
foreach ($posttags as $tag){
$id = $tag -> term_id ;
$ids[] = $id;
}
foreach ($ids as $id){
$args = array(
'tag_id' => $id,
);
$posts_array = get_posts( $args );
foreach( $posts_array as $post ) {
echo the_title();
}
}
}
}