PHP
Joomla! 6
Joomla! 5
Joomla! 4
Database
Basic
Filter Multiple IDs Safely with whereIn() and ParameterType
Replaces unsafe implode() IN (...) clauses with native whereIn() automatic parameter binding.
components/com_example/src/Model/ItemsModel.php
<?php
declare(strict_types=1);
use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\ParameterType;
$db = Factory::getContainer()->get(DatabaseInterface::class);
$ids = array_values(array_filter(array_map('intval', [12, 18, 25])));
if (!empty($ids)) {
$query = $db->getQuery(true)
->select([$db->quoteName('id'), $db->quoteName('title')])
->from($db->quoteName('#__content'))
->whereIn($db->quoteName('id'), $ids, ParameterType::INTEGER);
$items = $db->setQuery($query)->loadObjectList();
}
Guard with !empty($ids) before calling whereIn() so an empty array never fails.
Technical explanation and best practices
$query->whereIn() in Joomla 4, 5 and 6 automatically generates bound placeholders for every element in an integer or string array.