I want to show similar posts on the post page using this code:
$tagsArray = array_map('trim', explode(',', $tags));
$tagsString = '';
foreach ($tagsArray as $tag) {
$tagsString .= "tags LIKE '%{$tag}%' OR ";
}
$tagsString = rtrim($tagsString, 'OR ');
$data = array(
'id' => $id,
);
$sql = $this->conn->prepare("SELECT * FROM ps_objects WHERE id != :id AND {$tagsString} ORDER BY date DESC LIMIT 5;");
$sql->execute($data);
$result = $sql->fetchAll();
The problem is that the post which has one tag for example john are shown all posts which have tags john or john,Leo,Mike but on the page of the post which has several tags for example john,Leo is not shown anything and should be shown all posts where there is at least one tag john or Leo in any sequence.
Help to write such SQL
Trying FIND_IN_SET didn’t help.