Do multiple jobs on date change without WordPress cron job

I m trying to randomize several posts on my client’s site which are on different pages , everyday. So I wrote a function which returns true on date change, and if does, I randomize the post ids and save them into the database and use the same values throughout the day, So here is what I m doing,

function comi_date_change(){ 
    date_default_timezone_set('Asia/Kolkata');
    $current_date = date('j F Y');
    $comi_current_date = get_option('comi_current_date');
    if($current_date == $comi_current_date){ 
        return FALSE;
    }else{
        update_option('comi_current_date', $current_date );  
        return TRUE;
    }
}

When I check the condition mocomi_date_chage() to be true or false, it works but only once, because, the moment this function executes, it saves the current date into the database and it returns false for the rest?
I don’t want to use WordPress cron job function wp_schedule_event(), is there any alternative?

Read More

Thanks

Related posts

2 comments

  1. Another way to repeat a task regularly – but not on every page refresh – without using cron is to use a transient that lasts for a day. It will even help your website display its homepage faster!!

    A transient is a wordpress way to cache bits of html. It is especially useful for complex requests to the database. With transient, you do the complex requests, then store the html output in the database for a given amount of time. So you could run your function, store its output and have it cached for X amounts of seconds (a day = 24*60*60 seconds = 86400).

    Here is a (reduced) example of what you should run on your front-page.php template.

    // Get any existing copy of our transient data
    if ( false === ( $special_query_results = get_transient( 'frontpage_random_posts' ) ) ) {
        // It wasn't there, so regenerate the data and save the transient
         $special_query_results = new WP_Query( 'cat=5&order=random&limit=1&orderby=date' );
         set_transient( 'frontpage_random_posts', $special_query_results, 86400 );
    }
    
    // Use the data like you would have normally...
    

    More info: http://wp.tutsplus.com/tutorials/getting-started-with-the-wordpress-transients-api-part-1/

  2. Since I have to check 8 conditions this is what I m doing now.

    add_action('init', 'comi_date_change');
    function comi_date_change(){ 
        date_default_timezone_set('Asia/Kolkata');
        $current_date = date('j F Y');
        $comi_current_date = get_option('comi_current_date');
    
        if($current_date !== $comi_current_date){
            update_option('comi_current_date', $current_date );            
            update_option('dc_home_mustview', '2');
            update_option('dc_fun_mustview', '2');
            update_option('dc_learn_mustview', '2');
            update_option('dc_fun_subcat_one', '2');
            update_option('dc_fun_subcat_two', '2');
            update_option('dc_learn_subcat_one', '2');
            update_option('dc_home_learn_subcat_two', '2');
            update_option('dc_editors_picks', '2');
            update_option('dc_home_factory', '2');
        }
    }
    

    On each date change , it sets value 2 to each key, and then I can individually check each one of them for every conditions.

    $date_change = get_option('dc_home_mustview');
    if($date_change ==  2){
    //do stuff
    update_option('dc_home_mustview', 1);
    }
    

    and same for the rest conditions on different pages.

Comments are closed.