[wp-trac] [WordPress Trac] #63987: wp_get_scheduled_event() incorrectly returns false for cron events scheduled with a timestamp of 0.
WordPress Trac
noreply at wordpress.org
Tue Sep 16 09:36:31 UTC 2025
#63987: wp_get_scheduled_event() incorrectly returns false for cron events
scheduled with a timestamp of 0.
--------------------------+-----------------------------
Reporter: codekraft | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Cron API | Version: trunk
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
When using the wp_get_scheduled_event() function to retrieve a scheduled
cron event, it incorrectly returns false if the event's timestamp is
exactly 0 (the Unix epoch).
wp_get_scheduled_event can be found in wp-includes/cron.php:787
**The issue**
The issue lies in this line:
{{{#!php
<?php
if ( ! $timestamp ) {
}}}
In PHP, the integer 0 is evaluated as false in a boolean context. This
causes the function to enter the "get next event" block, which then fails
to find the event at timestamp 0 and ultimately returns false, despite the
event existing in the cron array.
**How to Reproduce**
Schedule a cron event with a timestamp of 0. You can do this by manually
adding it to the cron array or by using a function like this:
{{{#!php
<?php
// Add an event with a timestamp of 0.
// This is for demonstration purposes. In a real scenario, this might
happen due to a bug in a plugin.
$crons = _get_cron_array();
$crons[0]['my_test_hook']['unique_key'] = array(
'schedule' => false,
'args' => array(),
);
_set_cron_array($crons);
}}}
Attempt to retrieve this specific event using wp_get_scheduled_event()
with the timestamp 0:
{{{#!php
<?php
$event = wp_get_scheduled_event( 'my_test_hook', array(), 0 );
}}}
Check the result.
{{{#!php
<?php
if ( false === $event ) {
echo "The function returned false. This is the bug.";
} else {
echo "The function correctly found the event.";
}
}}}
Expected behavior: The function should return the event object, as the
event with a timestamp of 0 exists.
Current behavior: The function returns false, indicating the event does
not exist. So i cannot remove that event from the cron array using
`wp_unschedule_event`
**Suggested Fix**
A possible fix is to change the if condition to be more explicit, for
example:
{{{#!php
<?php
if ( null === $timestamp ) {
}}}
This would only trigger the "get next event" logic when no timestamp is
provided, correctly handling a timestamp of 0 as a valid value.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/63987>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list