1

I'm creating a custom search form in Joomla! to search articles from certain categories.

Here is my query:

$categories = array('2', '12', '16');
$query
    ->select(array('id', 'title', 'catid'))
    ->from('#__content')
    ->where($db->quoteName('title') . ' LIKE ' . $db->quote('%' . $searchterm . '%'), 'OR')
    ->where($db->quoteName('introtext') . ' LIKE ' . $db->quote('%' . $searchterm . '%'))
    ->where($db->quoteName('catid') . ' IN (' . implode(',', $categories) . ')');

I tried it this way too:

$categories = array('2', '12', '16');
$query
    ->select(array('id', 'title', 'catid'))
    ->from('#__content')
    ->where($db->quoteName('catid') . ' IN (' . implode(',', $categories) . ')')
    ->where($db->quoteName('title') . ' LIKE ' . $db->quote('%' . $searchterm . '%'), 'OR')
    ->where($db->quoteName('introtext') . ' LIKE ' . $db->quote('%' . $searchterm . '%'));

Issue: It's showing results from all categories instead of specified categories.

2

2 Answers 2

2

Use extendWhere() method with AND as outer glue and OR as inner glue:

$query
    ->where($db->quoteName('catid') . ' IN (' . implode(',', $categories) . ')')
    ->extendWhere(
        'AND',
        [
            $db->quoteName('title') . ' LIKE ' . $db->quote('%' . $searchterm . '%'),
            $db->quoteName('introtext') . ' LIKE ' . $db->quote('%' . $searchterm . '%'),
        ],
        'OR'
    );

Or the shorthand andWhere() method:

$query
    ->where($db->quoteName('catid') . ' IN (' . implode(',', $categories) . ')')
    ->andWhere(
        [
            $db->quoteName('title') . ' LIKE ' . $db->quote('%' . $searchterm . '%'),
            $db->quoteName('introtext') . ' LIKE ' . $db->quote('%' . $searchterm . '%'),
        ]
    );

The resulting query:

SELECT id,title,catid
FROM #__content
WHERE 
(`catid` IN (2,12,16)) AND 
(`title` LIKE '%searchterm%' OR `introtext` LIKE '%searchterm%')

Note: to use these methods there must be at least one where() call made before.

1
  • Wow! I was struggling hours but I didn't know there are andWhere and extendWhere available in Joomla! as I checked Joomla! documentation only.
    – Russell
    Sep 13, 2021 at 17:30
0

Try:

$query->orWhere($db->quoteName('title') . ' LIKE ' . $db->quote('%' . $searchterm . '%'));
1
  • Thank you for attempting to help, but please make a habit of including some plain English explanation with every answer that you post. Your answer was automatically flagged by the system as Low quality. Code-only answers do not typically help researchers to grow. In fact, if developers know they can come to Stack Exchange to mindlessly copy snippets, we will give birth to a generation of programmers who don't actually know what they are doing. Sep 13, 2021 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.