Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this code

is_admin() or add_action( 'the_post', function( $post ) {

if ( ! is_singular() or ! is_main_query() )
    return;

$post->post_date     = current_time( 'mysql' );
$post->post_date_gmt = '';

wp_update_post( $post );

});

When refreshing the page, it will update to current time. But this will slow down my site. So how to update date time of page to current time only 1 time per day ?

Example: I write a post on 5pm, June 6, 2013. And now, when I refresh page, it will change from 5pm, June 6 to 7pm, June 9. And in June 9, if I refresh the page, it will not change anything.

Sorry for my poor English ! Thank you very much !

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Compare the post_date with the current time and then test if the difference is greater than one day:

is_admin() or add_action( 'the_post', function( $post ) {

    if ( ! is_singular() or ! is_main_query() )
        return;

    // now minus last mod time in seconds
    $diff = time() - mysql2date( 'U', $post->post_date );

    if ( DAY_IN_SECONDS >= $diff )
        return;

    $post->post_date     = current_time( 'mysql' );
    $post->post_date_gmt = '';

    wp_update_post( $post );
});
share|improve this answer
    
Thank you very much ! Great ! –  Nguyên Bama Jun 9 '13 at 15:45

Your Answer

 
discard

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.