I am trying to get entries that either their title has the keyword or their category field title. I am getting them from my plugin, not twig. I tried like this:

$keywordCategory= Category::find()->title($keyword)->all();
$entries = Entry::find()->title($keyword)->relatedTo($keywordCategory)->all();

but it must be in the title and the category field. Is there a way to check using where and orWhere?


You would have to create the relatedTo condition manually first and then pass it in to the orWhere method. This is untested, but should get you on the right track.

Reference the _applyRelatedToParam() method in the ElementQuery class: https://github.com/craftcms/cms/blob/3.1.26/src/elements/db/ElementQuery.php#L1724-L1727

$keywordCategory = Category::find()->title($keyword)->all();

$parser = new ElementRelationParamParser();
$relatedTo = $parser->parse($keywordCategory);

$entries = Entry::find()
    ->where(['title' => $keyword])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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