wp_get_schedule( string $hook, array $args = array() )

Retrieve the recurrence schedule for an event.


Description Description

See also See also


Top ↑

Parameters Parameters

$hook

(string) (Required) Action hook to identify the event.

$args

(array) (Optional) Arguments passed to the event's callback function.

Default value: array()


Top ↑

Return Return

(string|false) False, if no schedule. Schedule name on success.


Top ↑

Source Source

File: wp-includes/cron.php

function wp_get_schedule( $hook, $args = array() ) {
	$schedule = false;
	$event    = wp_get_scheduled_event( $hook, $args );

	if ( $event ) {
		$schedule = $event->schedule;
	}

	/**
	 * Filter the schedule for a hook.
	 *
	 * @since 5.1.0
	 *
	 * @param string|bool $schedule Schedule for the hook. False if not found.
	 * @param string      $hook     Action hook to execute when cron is run.
	 * @param array       $args     Optional. Arguments to pass to the hook's callback function.
	 */
	return apply_filters( 'get_schedule', $schedule, $hook, $args );
}

Top ↑

Changelog Changelog

Changelog
Version Description
5.1.0 'get_schedule' filter added.
2.1.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Andrei G.

    I suggest that maybe the documentation here should be updated to say that the function returns false if no RECURRENCE.

    I came across an issue lately with a plugin that was scheduling a single event and using wp_get_schedule() to check for the existence of a schedule.

    		if ( !wp_get_schedule( 'groups_file_access_session_delete_transients' )) {
    			
    			wp_schedule_single_event( time() + self::SCHEDULE, 'groups_file_access_session_delete_transients' );
    		}

    It took me a while to understand that wp_get_schedule() does not return the actual timestamp like wp_next_scheduled(), but the recurrence value, if any (hourly, daily, etc.).

    As such, in the example above, the scheduling of single events was happening on every single call, causing for the cron field in the database to become gigantic.

    Since wp_get_schedule() seems to not see single events (since they have no recurrence) and will always return false for single events, it is somewhat confusing.

    I suspect people would expect for wp_get_schedule() to work like wp_next_scheduled(), but that’s not the case.

  2. Skip to note 2 content
    Contributed by Codex

    Basic Examples

    // If you previously added for example:
    // wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event' );
    
    $schedule = wp_get_schedule( 'my_hourly_event' );
    
    // $schedule == 'hourly'
    
    // Or this if you created something like this:
    // wp_schedule_single_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event', array( 'some_arg' ) );
    
    $schedule = wp_get_schedule( 'my_hourly_event', array( 'some_arg' ) );
    
    // $schedule == 'hourly'
    

You must log in before being able to contribute a note or feedback.