2014-05-02 37 views
1

好的,這裏有: ACF有日期字段可應用於帖子,頁面等。我使用它來創建非常基本的事件,使用帖子,而不是頁面。我只需要使用帖子來舉辦活動。考慮到這一點,這裏是我的問題:Wordpress ACF - 自動刪除帖子,如果ACF日期字段比當前日期更早

我想知道是否有可能刪除PHP的帖子(在帖子模板中),這將(當它通過帖子循環時),刪除帖子,如果該帖子的ACF日期字段比當前日期早。

這似乎是本應該已經解決或追求的東西,但我沒有得到很好的谷歌搜索結果。所以我猜這可能涉及cron作業或一些更深入的PHP /後端掌握?

通常我會做如下:

<?php 

// get posts 
$posts = get_posts(array(
    'post_type'  => 'post', 
    'posts_per_page' => -1, 
    'meta_key'  => 'start_date', 
    'orderby'  => 'meta_value_num', 
    'order'   => 'ASC' 
)); 

if($posts) 
{ 
    foreach($posts as $post) 
    { 

     // CODE to delete post if ACF date is old 

     $titleID = get_the_title($ID); 
     echo '<h3>' . $titleID . '</h3>'; 
     // I've removed some of the other stuff like links, 
     // excerpts, etc, to keep this simple. 

    } 
} 

>

我不只是要過濾掉舊的事件,我想他們走了,刪除(保留DB光? )。

理想情況下,我寧願不必手動刪除舊事件。

+0

我認爲你正在尋找['wp_trash_post()'](https://codex.wordpress.org/Function_Reference/ wp_trash_post) – cpilko

回答

4

你可以使用wp_delete_post()刪除帖子,其start_date太舊:

// get a timestamp from the time string 
$post_date = strtotime($post->start_date); 
// check if the start_date is older than 1 week 
if (((time() - $post_date) > (7 * 24 * 60 * 60))) { 
    // to remove the post directly i.e. not moving it to trash 
    // you could set the second argument to true 
    wp_delete_post($post->ID); 
} else { 
    print '<h3>' . $post->post_title . '</h3>'; 
} 
+1

非常好。非常感謝! – 3Dom

+1

我結束了只改變1或2的東西: $ post_date = strtotime(get_field('end_date')); 所以我用end_date代替。如果結束日期超過1天,就刪除帖子。再次感謝。你認真地做了我的一週。 – 3Dom

+0

@ 3Dom - 很高興爲你解決。 – Cyclonecode

相關問題