I want to update some custom field values on my Entries with data from a third-party API, via a Plugin or Module.

How do I load the relevant Entries, and one-by-one set and save the new data in my Plugin/Module? There could be 1500+ Entries in each operation.

I don't mind at this stage if the logic is in a Controller or Service. Ideally, this work could be triggered from the CP.

I'm familiar with Element/Entry events for saving, etc. but can’t find a starting point outside the normal Element lifecycle.

1 Answer 1


You can use entry queries to fetch the entries and the elements service to save them. You can access the element service using Craft::$app->elements. Use Craft::$app->elements->saveElement($entry); to save an entry (or any other element).

The best place to put business logic is a service class that you can then call from your controllers. So something like this:

use Craft;
use craft\elements\Entry;

public function updateEntriesWithCustomInfo()
    /** @var craft\services\Elements $elementService */
    $elementService = Craft::$app->elements;

    // add conditions here
    $entries = Entry::find()->limit(1500)->all();

    foreach ($entries as $entry) {
        // update your field
        $entry->setFieldValue('my_custom_field', 'some_value');
        $success = $elementService->saveElement($entry);
        if (!$success) {
            // saving failed, log error or abort

Keep in mind that this might take a while if you have to process 1500 entries at once. If this isn't a one-off, it would be better to put this in a custom queue job. Then your service method would just find the entries to update and add queue jobs with their IDs to the queue. The queue job can either process one element at a time or a batch of elements (e.g. in batches of 25), depending on your requirements.

  • thanks @MoritzLost
    – joomkit
    Commented Sep 14, 2021 at 10:15

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.