Set sticky posts schedule (Automatic)

OK I got it working this is the code I used, I am not sure if it is the best practice but it certainly works.

In functions.php where I make the posts sticky from the front-end I added this to update the post_date to today so I can have control over the sticky period:

$newdate = current_time('mysql');
$my_post = array(
    'ID' => $post_id,
    'post_date' => $newdate
);
wp_update_post($my_post);

In functions.php (I used -1 day for testing purpose, if you type -7 days on the $expire variable it will unstick stickyposts older than 7 days.

//Delete Old Stickies START
function deleteOldStickies($post_id, $postDate) {
    $postDate = strtotime($postDate);
    $expire = strtotime('-1 day') + get_option( 'gmt_offset' ) * 3600;
    if ($postDate < $expire && is_sticky()) {
        unstick_post($post_id);
    }
}

//Delete Old Stickies END

In the loop :

<?php echo deleteOldStickies($post->ID, $post->post_date); ?>

Leave a Comment