简体   繁体   English

WordPress ACF-如果ACF日期字段早于当前日期,则自动删除帖子

[英]Wordpress ACF - Auto delete posts if ACF date field is older than current date

Ok, here goes: ACF has date fields which can be applied to posts, pages, etc. I'm using this to create very basic events, using posts, not pages. 好的,这是可行的:ACF具有可以应用于帖子,页面等的日期字段。我正在使用它来创建非常基本的事件,而不是使用帖子而不是页面。 I only ever need to use posts for events. 我只需要为事件使用帖子。 With this in mind, here is my question: 考虑到这一点,这是我的问题:

I want to know whether it is possible to delete posts with PHP (within the post template), that will (while it loops through the posts), delete the post if the ACF date field for that post is older than the current date. 我想知道是否有可能使用PHP删除帖子(在帖子模板内),如果该帖子的ACF日期字段早于当前日期,则将删除该帖子(同时在帖子中循环)。

This seems like something that should have been solved or sought after by now, but I'm not getting great Google results for this. 这似乎是应该已经解决或寻求的东西,但是我在Google上并没有取得很好的结果。 So I'm guessing this may involve cron jobs or some deeper PHP/back-end mastery? 所以我猜想这可能涉及cron作业或更深入的PHP /后端掌握?

Typically what I'd be doing is the following: 通常,我要做的是以下内容:

<?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.

    }
}

?> ?>

I do not just want to filter out old events, I want them gone, deleted (keep the DB light). 我不仅要过滤掉旧事件,还希望它们消失,删除(保持数据库亮起)。

Ideally I'd prefer not to have to delete old events manually. 理想情况下,我希望不必手动删除旧事件。

You could use wp_delete_post() to remove posts whose start_date is too old: 您可以使用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>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM