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 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(){ 
    $current_date = date('j F Y');
    $comi_current_date = get_option('comi_current_date');
    if($current_date == $comi_current_date){ 
        return FALSE;
        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?


share|improve this question
I think the code is right as it is returning true on date change, it will return true on next day. You can test it by changing date of your system if you are hosting it locally. – Vinod Dalvi Sep 11 '13 at 19:54

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/

share|improve this answer
Thanks pixeline, that's a great idea, I'll certainly try this, but I think that if a website is using W3 Total Cache, it already saves the content in cache folder so for now I changed my code a little. – Sayed Taqui Sep 19 '13 at 11:54

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

add_action('init', 'comi_date_change');
function comi_date_change(){ 
    $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.

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.