diff --git a/administrator/components/com_content/Model/ArticlesModel.php b/administrator/components/com_content/Model/ArticlesModel.php index 8a0ad9041bf4d..e38cf20dc6c32 100644 --- a/administrator/components/com_content/Model/ArticlesModel.php +++ b/administrator/components/com_content/Model/ArticlesModel.php @@ -19,6 +19,7 @@ use Joomla\CMS\Table\Table; use Joomla\CMS\Workflow\Workflow; use Joomla\Component\Content\Administrator\Extension\ContentComponent; +use Joomla\Database\ParameterType; use Joomla\Utilities\ArrayHelper; /** @@ -204,7 +205,7 @@ protected function getListQuery() $query->select( $this->getState( 'list.select', - 'DISTINCT a.id, a.title, a.alias, a.checked_out, a.checked_out_time, a.catid' . + 'a.id, a.title, a.alias, a.checked_out, a.checked_out_time, a.catid' . ', a.state, a.access, a.created, a.created_by, a.created_by_alias, a.modified, a.ordering, a.featured, a.language, a.hits' . ', a.publish_up, a.publish_down, a.introtext, a.note' ) @@ -461,34 +462,48 @@ protected function getListQuery() } // Filter by a single or group of tags. - $hasTag = false; - $tagId = $this->getState('filter.tag'); + $tag = $this->getState('filter.tag'); - if (is_numeric($tagId)) + // Run simplified query when filtering by one tag. + if (\is_array($tag) && \count($tag) === 1) { - $hasTag = true; - - $query->where($db->quoteName('tagmap.tag_id') . ' = ' . (int) $tagId); + $tag = $tag[0]; } - elseif (is_array($tagId)) + + if ($tag && \is_array($tag)) { - $tagId = ArrayHelper::toInteger($tagId); - $tagId = implode(',', $tagId); + $tag = ArrayHelper::toInteger($tag); - if (!empty($tagId)) - { - $hasTag = true; + $subQuery = $db->getQuery(true) + ->select('DISTINCT ' . $db->quoteName('content_item_id')) + ->from($db->quoteName('#__contentitem_tag_map')) + ->where( + [ + $db->quoteName('tag_id') . ' IN (' . implode(',', $query->bindArray($tag)) . ')', + $db->quoteName('type_alias') . ' = ' . $db->quote('com_content.article'), + ] + ); - $query->where($db->quoteName('tagmap.tag_id') . ' IN (' . $tagId . ')'); - } + $query->join( + 'INNER', + '(' . $subQuery . ') AS ' . $db->quoteName('tagmap'), + $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id') + ); } - - if ($hasTag) + elseif ($tag = (int) $tag) { - $query->join('LEFT', $db->quoteName('#__contentitem_tag_map', 'tagmap') - . ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id') - . ' AND ' . $db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_content.article') - ); + $query->join( + 'INNER', + $db->quoteName('#__contentitem_tag_map', 'tagmap'), + $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id') + ) + ->where( + [ + $db->quoteName('tagmap.tag_id') . ' = :tag', + $db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_content.article'), + ] + ) + ->bind(':tag', $tag, ParameterType::INTEGER); } // Add the list ordering clause.