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?

  • Anyone have any ideas?? – user2056505 Jun 26 '15 at 3:17
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; 

...
| improve this answer | |

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.