1

I'm trying to bring back a list of entries from a section. Currently I have the following:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'airports';
$criteria->airportcode = 'BCN';
$airportPrefHotels = craft()->elements->findElements($criteria);

The airport section has a multiple entries field. I want to loop over them.

Is it possible?

4
  • What type of field is airportcode? Also, you tagged this with craft3, but I assume you're referring to Craft 2 here, correct?
    – Brad Bell
    May 31 '17 at 17:58
  • It wouldn't let me add craft 2. airportCode is a text field inside the section. I want to access the entries inside the field attached to the airport section May 31 '17 at 18:01
  • So inside of an entry inside of a section with a handle of airports, you've got an [entries field] for relating other entries. What's the name of that field? And do you want to loop over all of those related entries for every parent entry?
    – Brad Bell
    May 31 '17 at 18:24
  • I’m assuming there only one airport matching that criteria? If that’s right see my answer below @Christophfen
    – carlcs
    May 31 '17 at 18:54
1

You first need to query for the “airport” entry by using the first() method. $airport->myEntriedFieldHandle then returns an ElementCriteriaModel prepared to get all the entries selected in the Entries field.

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'airports';
$criteria->airportcode = 'BCN';
$airport = $criteria->first();

if ($airport) {
    $criteria = $airport->prefHotels;
    $airportPrefHotels = $criteria->find();

    if ($airportPrefHotels) {
        // do useful things with $airportPrefHotels
    }
}

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.