wppaste
WordPress

wp_get_scheduled_event( string $hook, array $args = array(), int|null $timestamp = null ): object|false

Since
5.1.0
Source
wp-includes/cron.php:751
Retrieves a scheduled event.

Description

Retrieves the full event object for a given event, if no timestamp is specified the next scheduled event is returned.

Compatibility

WordPress
since 5.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$hookstring
Action hook of the event.
$argsarrayoptional
Array containing each separate argument to pass to the hook's callback function.
Although not passed to a callback, these arguments are used to uniquely identify the event, so they should be the same as those used when originally scheduling the event.
Default empty array.Default: array()
$timestampint|nulloptional
Unix timestamp (UTC) of the event. If not specified, the next scheduled event is returned. Default null.Default: null

Return value

object|false
The event object. False if the event does not exist.
  • $hookstring

    Action hook to execute when the event is run.
  • $timestampint

    Unix timestamp (UTC) for when to next run the event.
  • $schedulestring|false

    How often the event should subsequently recur.
  • $argsarray

    Array containing each separate argument to pass to the hook's callback function.
  • $intervalint

    Optional. The interval time in seconds for the schedule. Only present for recurring events.

Performance profile

How much work a call to wp_get_scheduled_event() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
14–63

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 72.

Plugin surface
1 hook

Third-party callbacks on 'pre_get_scheduled_event' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • serializeserialisationserialize()called directly
  • optionoption read or writeget_option()one call below wp_get_scheduled_event()

Further down the call graph this can also reach cache, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_scheduled_event() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always14–18apply_filters()
$pre === null && empty($crons)21–23apply_filters(), _get_cron_array()
$pre === null && !empty($crons)33–63apply_filters(), _get_cron_array(), serialize(), md5()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7214–6311
8.57214–6311
8.47214–63112 fewer instructions than PHP 8.3
8.37414–6511
8.27414–6511
8.17414–6511
7.47414–6511

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_get_scheduled_event() runs, in this order:

  1. apply_filters( pre_get_scheduled_event )filterline 770 (+19 into the body)

    Filter to override retrieving a scheduled event.

Uses · 2

Used by · 4

Source code

function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) {	/**	 * Filter to override retrieving a scheduled event.	 *	 * Returning a non-null value will short-circuit the normal process,	 * returning the filtered value instead.	 *	 * Return false if the event does not exist, otherwise an event object	 * should be returned.	 *	 * @since 5.1.0	 *	 * @param null|false|object $pre  Value to return instead. Default null to continue retrieving the event.	 * @param string            $hook Action hook of the event.	 * @param array             $args Array containing each separate argument to pass to the hook's callback function.	 *                                Although not passed to a callback, these arguments are used to uniquely identify	 *                                the event.	 * @param int|null  $timestamp Unix timestamp (UTC) of the event. Null to retrieve next scheduled event.	 */	$pre = apply_filters( 'pre_get_scheduled_event', null, $hook, $args, $timestamp ); 	if ( null !== $pre ) {		return $pre;	} 	if ( null !== $timestamp && ! is_numeric( $timestamp ) ) {		return false;	} 	$crons = _get_cron_array();	if ( empty( $crons ) ) {		return false;	} 	$key = md5( serialize( $args ) ); 	if ( ! $timestamp ) {		// Get next event.		$next = false;		foreach ( $crons as $timestamp => $cron ) {			if ( isset( $cron[ $hook ][ $key ] ) ) {				$next = $timestamp;				break;			}		} 		if ( ! $next ) {			return false;		} 		$timestamp = $next;	} elseif ( ! isset( $crons[ $timestamp ][ $hook ][ $key ] ) ) {		return false;	} 	$event = (object) array(		'hook'      => $hook,		'timestamp' => $timestamp,		'schedule'  => $crons[ $timestamp ][ $hook ][ $key ]['schedule'],		'args'      => $args,	); 	if ( isset( $crons[ $timestamp ][ $hook ][ $key ]['interval'] ) ) {		$event->interval = $crons[ $timestamp ][ $hook ][ $key ]['interval'];	} 	return $event;}

Changelog

Introduced in 5.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/cron.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.