0

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 !

0

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 );
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.