我已经建立了一个系统,在这里我有两个选择-草稿的生命卒和发布的帖子的生命卒(整数表示天数)。
如果发布帖子,则发布的确切日期和时间另存为postmeta
, 此外,lifespawn被添加到该时间中,该值作为过期日期时间保存到数据库中,这与draft的情况相同(保存帖子时也可以这样)。
我当前的系统是这样运行的:
我已经设置了一个服务器cron,它每24小时在夜间运行一次metadata
从中的数据库检索while
将该时间与中的当前时间进行比较的循环if
声明如果时间已过,请将职位状态更改为草稿,查询所有草稿职位的草稿日期时间metadata
从中的数据库检索while
将该时间与中的当前时间进行比较的循环if
声明
如果时间流逝,请删除帖子也会查询并删除帖子的所有附加图像//Current date-time
$now = date( \'Y-m-d H:i:s\' ); //Let\'s say it\'s 2015-12-24 21:00:00 (9PM)
//Get all published posts that are over the deadline
$published_posts = new WP_Query( array(
\'posts_per_page\' => -1,
\'post_type\' => \'my-post\',
\'post_status\' => \'publish\',
\'meta_query\' => array(
array(
\'key\' => \'post_publish_deadline\', //string e.g 2015-12-24 15:00:00 (3PM)
\'value\' => $now,
\'type\' => \'DATETIME\',
\'compare\' => \'<=\',
)
)
));
while( $published_posts->have_posts() ) {
$published_posts->the_post();
//Get postmeta deadline and format it for if statement
$publish_deadline = get_post_meta( get_the_ID(), \'post_publish_deadline\', true );
//If statement doesn\'t seem to work without this step
$format_publish_deadline = date( \'Y-m-d H:i:s\', strtotime( $publish_deadline ) );
//Check if it exists just in case
if( !empty( $format_publish_deadline ) ) {
//Additional check if time for that guy is over just in case
if( $format_publish_deadline < $now ) {
//Change post status to draft
wp_update_post( array( \'ID\' => get_the_ID(), \'post_status\' => \'draft\' ) );
//Get lifespawn option
$draft_to_delete_lifespawn = $my_option[ \'draft-lifespawn\' ];
//Build a new draft expiration date-time -> now + lifespawn in hours
$draft_deadline = date( \'Y-m-d H:i:s\', strtotime( $now . \' + \' . $draft_to_delete_lifespawn . \' hours\' ) );
//Save new draft expiration date-time
update_post_meta( $post->ID, \'post_draft_deadline\', $draft_deadline );
}
}
} //END while
wp_reset_postdata();