I have one function: "update_user_like" to sum ​​all facebook likes from all user's posts.

How to run that function at 3am every day by wp cron job?

Thanks much!

  • set the cron at 3am to repeat every 24 hours.
    – JMau
    Jun 20, 2013 at 17:09
  • I don't know how to write that code before :( Can you help me write down with a complete code? Thank you very much! Jun 20, 2013 at 17:12

1 Answer 1


First of all you have to remember how WP cron works. The action will trigger when someone visits your WordPress site, if the scheduled time has passed. So if your site isn't very crowded at 3am, the cron job won't start exactly at 3am. (You should use real cron if you really need to run it exactly at 3am).

To schedule an event at 3am every day you can use this code:

function do_this_daily_after_3am() {
    // do something every day after 3am
add_action('my_daily_event', 'do_this_daily_after_3am');

function my_activation() {
    if ( !wp_next_scheduled( 'my_daily_event' ) ) {
        $next3am = ( date('Hi') >= '0300' ) ? strtotime('+1day 3am') : strtotime('3am');  // you can calculate it in any way you want

        wp_schedule_single_event( $next3am, 'my_daily_event');
add_action('wp', 'my_activation');
  • Thanks you! I will try it and tell you the the results its works. Jun 21, 2013 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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