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.
LIKE
SQL Injections using Jfactory::getDBO and setQuery() with LIKE condition Relavant insights about SQL operator precedence: Joomla Select from DB AND / OR grouping and Combining 'ANDs' and 'ORs' in a complex WHERE query - Part 2