As stated in a comment, your get_posts
parameters are wrong. There is no such parameter as term_id
. You should make use tag
which uses the tag slugs as a string or tag__in
which uses the tag ids in an array
This will work
$posts = get_posts('numberposts=6&tag='. $tags->slug);
or this will work
$posts = get_posts('numberposts=6&tag__in=' . array( $tags->term_id ));
There are some other issues as well that I want to highlight here
-
You should set the post ID in
get_the_tags()
. Do something like thisget_the_tags( $post->ID );
-
Set the plural name as the variable name which will hold your array from your function and use the single name as your
value
in yourforeach
loop. This eliminates confusion and is easier to debug. If you read this,$tags->name
, you immediately think of more than one tag, yet this is used to retrieve the specific tag’s name as$tags
are actually your value. You should do something like this which IMO is far better to understand and less confusing$tags = get_the_tags(); foreach ( $tags as $tag ){ echo $tag->name; }
EDIT
To exclude the current post, you can do the following
$posts = get_posts('numberposts=6&tag__in=' . array( $tags->term_id ) . '&post__not_in=' . array( $post->ID) );