4

So,

I want to grab all entries that are related to an entry but with the following criteria:

  1. Only from my section ($section)
  2. Only the ones that have been added to a certain field ($fieldId)

I currently have this:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = $section;
$criteria->limit   = null;
$criteria->relatedTo = array(
    'targetElement' => $element,
);
$elements = craft()->elements->findElements($criteria);
return $elements;

But if I add two relationship fields to an entry, this will return ALL the associated entries from either field. I want to limit it to just the entries attached to the one field.

Is this possible? Does the question make sense? :-)

4

If you want to limit your relationships to a particular field, use this syntax:

$criteria->relatedTo = array(
    'targetElement' => $element,
    'field' => $field
);

I believe 'field' will take:

1) A single or array of $field instances

2) A single or array of field IDs or field handles.

| improve this answer | |
  • That did! Appreciate it Brad. – Peter Tell Jul 11 '14 at 0:12

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.