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 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!

share|improve this question
set the cron at 3am to repeat every 24 hours. – JMau Jun 20 '13 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! – Zen Nguyễn Jun 20 '13 at 17:12

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');
share|improve this answer
Thanks you! I will try it and tell you the the results its works. – Zen Nguyễn Jun 21 '13 at 2:17

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.