函数文档

wp_scheduled_delete()

💡 云策文档标注

概述

wp_scheduled_delete() 是 WordPress 核心函数,用于永久删除在回收站中停留超过 EMPTY_TRASH_DAYS 天数的文章和评论。该函数通过定时任务自动清理,默认 EMPTY_TRASH_DAYS 为 30 天。

关键要点

  • 函数 wp_scheduled_delete() 永久删除状态为 'trash' 的文章和评论,基于 EMPTY_TRASH_DAYS 常量定义的天数阈值。
  • 默认 EMPTY_TRASH_DAYS 值为 30 天,可通过定义常量或过滤器修改。
  • 函数内部使用 SQL 查询获取待删除项,并调用 wp_delete_post() 和 wp_delete_comment() 等函数执行删除操作。
  • 删除前会检查文章或评论的当前状态,确保仅处理仍处于 'trash' 状态的项。

代码示例

function wp_scheduled_delete() {
	global $wpdb;

	$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );

	$posts_to_delete = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp ) );
	foreach ( $posts_to_delete as $post ) {
		$post_id = (int) $post->post_id;
		$post_status = get_post_status( $post_id );
		if ( 'trash' !== $post_status ) {
			delete_post_meta( $post_id, '_wp_trash_meta_status' );
			delete_post_meta( $post_id, '_wp_trash_meta_time' );
		} else {
			wp_delete_post( $post_id );
		}
	}

	$comments_to_delete = $wpdb->get_results( $wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp ) );
	foreach ( $comments_to_delete as $comment ) {
		$comment_id = (int) $comment->comment_id;
		$comment_status = get_comment_status( $comment_id );
		if ( 'trash' !== $comment_status ) {
			delete_comment_meta( $comment_id, '_wp_trash_meta_time' );
			delete_comment_meta( $comment_id, '_wp_trash_meta_status' );
		} else {
			wp_delete_comment( $del_comment );
		}
	}
}

注意事项

  • 此函数通常由 WordPress 定时任务(如 wp_cron)调用,开发者不应直接调用以避免意外数据丢失。
  • 修改 EMPTY_TRASH_DAYS 常量或使用过滤器可以调整清理周期,但需谨慎操作以符合数据保留策略。
  • 函数依赖于 _wp_trash_meta_time 和 _wp_trash_meta_status 元数据来跟踪回收站项,确保这些元数据正确设置。

📄 原文内容

Permanently deletes comments or posts of any type that have held a status of ‘trash’ for the number of days defined in EMPTY_TRASH_DAYS.

Description

The default value of EMPTY_TRASH_DAYS is 30 (days).

Source

function wp_scheduled_delete() {
	global $wpdb;

	$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );

	$posts_to_delete = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp ), ARRAY_A );

	foreach ( (array) $posts_to_delete as $post ) {
		$post_id = (int) $post['post_id'];
		if ( ! $post_id ) {
			continue;
		}

		$del_post = get_post( $post_id );

		if ( ! $del_post || 'trash' !== $del_post->post_status ) {
			delete_post_meta( $post_id, '_wp_trash_meta_status' );
			delete_post_meta( $post_id, '_wp_trash_meta_time' );
		} else {
			wp_delete_post( $post_id );
		}
	}

	$comments_to_delete = $wpdb->get_results( $wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp ), ARRAY_A );

	foreach ( (array) $comments_to_delete as $comment ) {
		$comment_id = (int) $comment['comment_id'];
		if ( ! $comment_id ) {
			continue;
		}

		$del_comment = get_comment( $comment_id );

		if ( ! $del_comment || 'trash' !== $del_comment->comment_approved ) {
			delete_comment_meta( $comment_id, '_wp_trash_meta_time' );
			delete_comment_meta( $comment_id, '_wp_trash_meta_status' );
		} else {
			wp_delete_comment( $del_comment );
		}
	}
}

Changelog

Version Description
2.9.0 Introduced.