$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>cron - Delete all scheduled events with a particular hook|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

cron - Delete all scheduled events with a particular hook

matteradmin10PV0评论

I have several cron jobs all hooked to the same hook, but each with a different argument (a post ID). It appears wp_clear_scheduled_hook only clears job which match the argument passed with it (or jobs without any arguments, if no argument is provided).

Is there a way to delete all the cron jobs associated with a hook, regardless of the argument that job has? (Without doing so 'manually' by looping through the IDs).


Seems an answer was deleted! To clarify what I would like to achieve: each post has an expiration date - and I would like to delete the post after this date.

(An alternative option is to have one cron job - repeating every 24 hours that deletes any expired posts).

However, I decided to create a one-off job for each post - but it seems you can't delete all the jobs at once without cycling through them.

I have several cron jobs all hooked to the same hook, but each with a different argument (a post ID). It appears wp_clear_scheduled_hook only clears job which match the argument passed with it (or jobs without any arguments, if no argument is provided).

Is there a way to delete all the cron jobs associated with a hook, regardless of the argument that job has? (Without doing so 'manually' by looping through the IDs).


Seems an answer was deleted! To clarify what I would like to achieve: each post has an expiration date - and I would like to delete the post after this date.

(An alternative option is to have one cron job - repeating every 24 hours that deletes any expired posts).

However, I decided to create a one-off job for each post - but it seems you can't delete all the jobs at once without cycling through them.

Share Improve this question edited Jan 21, 2012 at 17:18 Stephen Harris asked Jan 21, 2012 at 16:29 Stephen HarrisStephen Harris 32.7k6 gold badges84 silver badges118 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

4.9.0 introduced wp_unschedule_hook()

https://developer.wordpress/reference/functions/wp_unschedule_hook/

I have just quickly wrote the below function, it will clear all crons for the specified hook, irrespective of the cron time and the hook argument.

NOTE: I have NOT TESTED the function, so please don't run it on your live site.

function wpse39681_clear_all_crons( $hook ) {
    $crons = _get_cron_array();
    if ( empty( $crons ) ) {
        return;
    }
    foreach( $crons as $timestamp => $cron ) {
        if ( ! empty( $cron[$hook] ) )  {
            unset( $crons[$timestamp][$hook] );
        }

        if ( empty( $crons[$timestamp] ) ) {
            unset( $crons[$timestamp] );
        }
    }
    _set_cron_array( $crons );
}

Related: http://core.trac.wordpress/ticket/18997

Post a comment

comment list (0)

  1. No comments so far