WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm building a site for a client that requires sending out emails about each post at various dates (i.e. two weeks before after the post was published, then again four weeks after the post was published, etc). It seems that the best way to do this is with wp_schedule_single_event() with a unique event name, but I'm not quite sure how to properly call add_action on these single events when each event has a unique name.

I am scheduling the events like so:

add_action('wp_insert_post', 'cron_emails');
function cron_emails()  {
    global $post;
    $meta = get_post_custom($post->ID);

    // one day before event
    wp_clear_scheduled_hook("one_day_before_event_" . $post->ID);

    if(!empty($meta["_start-date"][0]))  {
        $newdate = strtotime ( '-1 day' , strtotime ( $meta["_start-date"][0] ) );
        wp_schedule_single_event($newdate, "one_day_before_event_" . $post->ID);    

This schedules a single event with the unique name one_day_before_event_[POST_ID]. Works great, no complaints. But now, when the cron rolls around, how do I trigger this unique event with an add_action? I don't really understand how to dynamically do an add_action when each post has a unique event name, like so:


The two options I can think of are (1) Loop through every single post id on the blog or (2) only call the action on single.php with:

is(is_single()) add_action('one_day_before_event_' . $post->ID, 'some_function');

Would anyone have any better ideas on how I might dynamically call an add_action for a unique event name?

share|improve this question
up vote 2 down vote accepted

You can pass arguments to the wp_schedule_single_event function:

wp_schedule_single_event($newdate, "one_day_before_event", array($post->ID));

Then if you do an add_action you can say how many arguments it will receive (in this case 1).

add_action( 'one_day_before_event', 'some_function', 10, 1 );

Then in your function you can access the variable:

function some_function($post_id) {
   // Do something with $post_id
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.