How can I conditionally resave entries based on the value of an entry's field in a Craft 3 Module/Plugin? Scenario: I want to programatically bulk resave entries in a certain Site in a certain Section of a certain Type.

I have a Craft 3 Module with a Console Controller containing following code:

public function actionResaveEntries()
{
    $site = Craft::$app-&gt;sites-&gt;getSiteById(self::SITE_ID);
    $section = Craft::$app-&gt;sections-&gt;getSectionById(self::SECTION_ID);
    $entryType = Craft::$app-&gt;sections-&gt;getEntryTypeById(self::ENTRY_TYPE_ID);

    Craft::$app-&gt;getQueue()-&gt;push(new ResaveElements([
        'description' =&gt; Craft::t('app', 'Resaving {section}, {type} entries', [
            'section' =&gt; $section, 'type' =&gt; $entryType-&gt;name,
        ]),
        'elementType' =&gt; Entry::class,
        'criteria' =&gt; [
            'siteId' =&gt; $site-&gt;id,
            'sectionId' =&gt; $section-&gt;id,
            'typeId' =&gt; $entryType-&gt;id,
            'status' =&gt; null,
            'enabledForSite' =&gt; true
        ]
    ]));

    $result = 'something';
    echo "Resaving entries\n";
    return $result;
}


This works as expected when running ./craft resave-entries-module/resave-entries/resave-entries from the command line &amp; also via a cron job.

However, I now want to modify my code to run Craft::$app-&gt;getQueue()-&gt;push(new ResaveElements() only under certain conditions. Specifically, when a deadline (custom date/time field in entry type) has passed.

I tried following, but it doesn't run at all regardless if before or after deadline date. The conditional seems to be ignored.

public function actionResaveEntries()
{
    $site = Craft::$app-&gt;sites-&gt;getSiteById(self::SITE_ID);
    $section = Craft::$app-&gt;sections-&gt;getSectionById(self::SECTION_ID);
    $entryType = Craft::$app-&gt;sections-&gt;getEntryTypeById(self::ENTRY_TYPE_ID);

    $entries = Entry::find()
        -&gt;siteId(self::SITE_ID)
        -&gt;sectionId(self::SECTION_ID)
        -&gt;typeId(self::COMP_ENTRY_TYPE_ID)
        -&gt;all();

    foreach ($entries as $entry) {
        // Set deadline
        $deadline = $entry-&gt;competitionJudgingEnd;
        // Check if after deadline
        $now = new DateTime();
        if ($now &gt; $deadline) {
            Craft::$app-&gt;getQueue()-&gt;push(new ResaveElements([
                'description' =&gt; Craft::t('app', 'Resaving {section}, {type} entries', [
                    'section' =&gt; $section, 'type' =&gt; $entryType-&gt;name,
                ]),
                'elementType' =&gt; Entry::class,
                'criteria' =&gt; [
                    'siteId' =&gt; $site-&gt;id,
                    'sectionId' =&gt; $section-&gt;id,
                    'typeId' =&gt; $entryType-&gt;id,
                    'status' =&gt; null,
                    'enabledForSite' =&gt; true
                ]
            ]));

            //$result = 'something';
            echo "Resaving entries\n";
            //return $result;
        } else {
            //$result = 'nothing';
            echo "Not resaving entries\n";
            //return $result;
        }
    }


In an effort to troubleshoot, I changed the conditional to if ($entry-&gt;title == "A known title"), but it also seems to be ignored. I.e. resave not occurring.

What am I doing wrong, and what can I do to get this working?

UPDATE 01: The conditionals are actually working. There are 3 entries that meet criteria in $entries = Entry::find(). However, the loop seems to be stop at the first entry. This entry changes if I change the criteria.

UPDATE 02: It looks like the return $result; was breaking the loop. Updated code above by commenting out return statements.
My working solution:

public function actionResaveEntries()
{
    $site = Craft::$app-&gt;sites-&gt;getSiteById(self::SITE_ID);
    $section = Craft::$app-&gt;sections-&gt;getSectionById(self::SECTION_ID);
    $entryType = Craft::$app-&gt;sections-&gt;getEntryTypeById(self::ENTRY_TYPE_ID);

    $entries = Entry::find()
        -&gt;siteId(self::SITE_ID)
        -&gt;sectionId(self::SECTION_ID)
        -&gt;typeId(self::COMP_ENTRY_TYPE_ID)
        -&gt;all();

    foreach ($entries as $entry) {
        // Set deadline
        $deadline = $entry-&gt;competitionJudgingEnd;
        // Check if after deadline
        $now = new DateTime();
        if ($now &gt; $deadline) {
            Craft::$app-&gt;getQueue()-&gt;push(new ResaveElements([
                'description' =&gt; Craft::t('app', 'Resaving {section}, {type} entries', [
                    'section' =&gt; $section, 'type' =&gt; $entryType-&gt;name,
                ]),
                'elementType' =&gt; Entry::class,
                'criteria' =&gt; [
                    'siteId' =&gt; $site-&gt;id,
                    'sectionId' =&gt; $section-&gt;id,
                    'typeId' =&gt; $entryType-&gt;id,
                    'status' =&gt; null,
                    'enabledForSite' =&gt; true
                ]
            ]));
            echo "Resaving entries\n"
        } else 
            echo "Not resaving entries\n";
        }
    }
}