Pergunta

EDIT: It is currently working when I set a sticky post and set it's date at 07/07/2016. My code so far: I've added this to change the post date of the post when it become sticky :

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

And also this to unstick the post if it is sticky and older than 7 days :

function deleteOldStickies($post_id, $postDate) {
    $postDate = strtotime($postDate);
    $currentTime = strtotime(time());
    $expire = $currentTime + strtotime('-1 day');
    if ($postDate < $expire && is_sticky()) {
        unstick_post($post_id);
        echo 'Обявата е изтекла!';
    }
}

In content.php I call the function as :

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

My further question from the info above is - am I missing something important, will it work good in live enviroment?

Foi útil?

Solução

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); ?>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top