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?
Thanks
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.
More info: http://wp.tutsplus.com/tutorials/getting-started-with-the-wordpress-transients-api-part-1/
Since I have to check 8 conditions this is what I m doing now.
On each date change , it sets value 2 to each key, and then I can individually check each one of them for every conditions.
and same for the rest conditions on different pages.