diff --git a/src/Repository/ContentRepository.php b/src/Repository/ContentRepository.php index 7223a160b..a742660fb 100644 --- a/src/Repository/ContentRepository.php +++ b/src/Repository/ContentRepository.php @@ -4,6 +4,7 @@ namespace Bolt\Repository; +use Bolt\Common\Str; use Bolt\Configuration\Content\ContentType; use Bolt\Doctrine\JsonHelper; use Bolt\Entity\Content; @@ -98,6 +99,10 @@ public function searchNaive(string $searchTerm, int $page, int $amountPerPage, C $connection = $qb->getEntityManager()->getConnection(); [$where] = JsonHelper::wrapJsonFunction('t.value', $searchTerm, $connection); + // Rather than searching for '%foo bar%', search '%foo%bar%' which doesn't require + // an exact match, but requires 'foo' to appear before 'bar'. + $searchTerm = str_replace(' ', '%', Str::cleanWhitespace($searchTerm)); + // The search term must match the format of the content in the database // Therefore, it is JSON encoded and escaped with backslashes $encodedSearchTerm = addslashes(trim(json_encode($searchTerm), '"'));