2

I got an asset field called docs which is contained in 2 different entries.
Those entries are in different sections and they contain the docs field directly or through a matrix field called articleBody.

I made two queries relatedTo and merge them thereafter.
It works but would it be a better way by combining the two queries in one relatedTo call?

Query 1: pages section entries contain docs field inside a matrix field called articleBody
Query 2: library section entries contain docs field directly

        $criteria1 = craft()->elements->getCriteria(ElementType::Entry);
        $criteria1->section = 'pages';
        $criteria1->relatedTo = array(
            'targetElement' => $asset->id,
            'field' => 'articleBody.docs'
        );

        $criteria2 = craft()->elements->getCriteria(ElementType::Entry);
        $criteria2->section = 'library';
        $criteria2->relatedTo = array(
            'targetElement' => $asset->id,
            'field' => 'docs'
        );

        $criteria = array_merge($criteria1, $criteria2);
2

OK in fact it was very simple: criteria params accept arrays...

        $criteria = craft()->elements->getCriteria(ElementType::Entry);

        $fields = [
            'articleBody.efficyDocuments',
            'efficyDocuments'
        ];

        $criteria->relatedTo = array(
            'targetElement' => $asset->id,
            'field' => $fields,
        );

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.