4

I have Structure-type Section for entries and want to pragmatically populate a large list of multi-tiered entries.

I see you can create an entry like:

$entry = new EntryModel();
$entry->getContent()->title = 'New Page';
$entry->getContent()->body = null;

craft()->entries->saveEntry($entry);

What I want to do is add the parent(s) of the entry, something like:

$entry = new EntryModel();
$entry->getContent()->title = 'New Page';
$entry->getContent()->body = null;

// this entry would be a child in order of Sports -> Tennis -> This Entry
$entry->getContent()->parent = sports->tennis;

craft()->entries->saveEntry($entry);

Is there a way to do something like that?

1
  • Anyone have any ideas?? Commented Jun 26, 2015 at 3:17

1 Answer 1

4

This question is old, but the solution is:

// look up parent with a ElementCriteriaModel

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

$parentCriteria->search  = 'slug:'.$parentSlug; // whatever you have

$parent = $parentCriteria->find()[0];

$entry = new EntryModel();
$entry->sectionId = $section;

$entry->parentId = $parent->id; 

...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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