[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
Sun Sep 21 11:23:43 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:  6.9
Component:  Cron API                             |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch has-unit-tests dev-        |     Focuses:
  feedback changes-requested has-test-info       |
-------------------------------------------------+-------------------------
Changes (by rollybueno):

 * keywords:  has-patch has-unit-tests dev-feedback changes-requested =>
     has-patch has-unit-tests dev-feedback changes-requested has-test-info


Comment:

 == Reproduction Report
 === Description
 This report validates whether the issue can be reproduced.

 The attached test plugin (`Ticket 63987`) injects two cron events for the
 same hook `my_test_hook`:
 - One at timestamp `0` (bug case).
 - One at a non-zero future timestamp (control case).

 When calling `wp_get_scheduled_event()` with timestamp `0`, the function
 incorrectly returns `false`.

 === Environment
 - WordPress: 6.9-alpha-60093-src
 - PHP: 8.2.29
 - Server: nginx/1.29.1
 - Database: mysqli (Server: 8.4.6 / Client: mysqlnd 8.2.29)
 - Browser: Chrome 139.0.0.0
 - OS: Linux
 - Theme: Twenty Twenty-Five 1.3
 - MU Plugins: None activated
 - Plugins:
   * Test Reports 1.2.0
   * Ticket 63987 1.0

 === Steps to Reproduce
 1. Activate the `Ticket 63987` plugin.
 2. Visit any front-end page with `?test-cron=1` appended to the URL.
 3. The plugin injects two events and dumps the results of
 `wp_get_scheduled_event()`.

 === Actual Results
 - For timestamp `0`:

   {{{
   bool(false)
   }}}

 - For the future timestamp:

   {{{
 object(stdClass)#311 (4) {
   ["hook"]=>
   string(12) "my_test_hook"
   ["timestamp"]=>
   int(1758456619)
   ["schedule"]=>
   bool(false)
   ["args"]=>
   array(0) {
   }
 }
   }}}

 ✅ Error condition occurs (reproduced).

 === Expected Results
 - For timestamp `0`, the event should be returned in the same format as
 the non-zero timestamp event. It should be:

 {{{
   object(stdClass)[312]
   public 'hook' => string 'my_test_hook' (length=12)
   public 'timestamp' => int 0
   public 'schedule' => boolean false
   public 'args' =>
     array (size=0)

 }}}

 - Returning `false` is incorrect since the event exists in the cron array.

 === Additional Notes
 - This confirms that the falsy check for `$timestamp` in
 `wp_get_scheduled_event()` causes timestamp `0` to be treated as "no
 timestamp."
 - Non-zero timestamps behave as expected.

 === Supplemental Artifacts
 Inline test plugin code for reproduction:

 {{{
 <?php
 /**
  * Plugin Name: Ticket 63987
  * Description: Demonstrates the bug in wp_get_scheduled_event() when
 timestamp is 0.
  * Version: 1.0
  * Author: Test
  */

 add_action( 'init', 'test_cron_zero_timestamp' );

 function test_cron_zero_timestamp() {
     if ( ! isset( $_GET['test-cron'] ) ) {
         return;
     }

     // 1. Inject a cron event at timestamp 0 (bug case).
     $crons = _get_cron_array();
     $args  = array();
     $key   = md5( serialize( $args ) );
     $crons[0]['my_test_hook'][ $key ] = array(
         'schedule' => false,
         'args'     => $args,
     );

     // 2. Inject a cron event at a non-zero timestamp (valid case).
     $future = time() + 3600; // 1 hour from now
     $crons[ $future ]['my_test_hook'][ $key ] = array(
         'schedule' => false,
         'args'     => $args,
     );

     // Save back into cron array.
     _set_cron_array( $crons );

     // 3. Try fetching both.
     $event_zero   = wp_get_scheduled_event( 'my_test_hook', array(), 0 );
     $event_future = wp_get_scheduled_event( 'my_test_hook', array(),
 $future );

     // 4. Output results.
     echo '<pre>';
     echo "Testing wp_get_scheduled_event()\n\n";

     echo "At timestamp 0:\n";
     var_dump( $event_zero );

     echo "\nAt timestamp {$future}:\n";
     var_dump( $event_future );

     echo '</pre>';

     exit;
 }
 }}}

 Actual result:
 [[Image(https://i.imgur.com/3nnCxiy.png)]]

 (**Correct result**) If https://github.com/WordPress/WordPress/blob/master
 /wp-includes/cron.php#L787 updated into `if ( null === $timestamp ) {`:
 [[Image(https://i.imgur.com/kBZYGx9.png)]]

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63987#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list