自动删除已发布的WordPress文章

huangapple go评论49阅读模式
英文:

Automatic deletion of published WordPress posts

问题

我已经创建了一个类似WordPress帖子的自定义帖子,我想每次(例如,每天或每周)自动删除它。有没有这样的功能?我知道可以使用以下功能删除垃圾帖子:

define('EMPTY_TRASH_DAYS', 10 );

但是对于自定义创建的帖子呢?

谢谢你的帮助。

英文:

I have created a custom post similar to WordPress posts that I want to delete automatically every time (for example, every day or every week).
Is there a function for this?
I know that you can delete trash posts with the following function

define('EMPTY_TRASH_DAYS', 10 );

But what about custom created posts?

Thanks for your help

答案1

得分: 0

你可以通过设置wp_schedule_event每周删除已发布的帖子。

希望这段代码能帮助你。

// 定义要自动删除的自定义文章类型
$custom_post_type = 'your_custom_post_type';

// 删除自定义文章类型文章的函数
function delete_custom_post_type_posts() {
    // 获取自定义文章类型的所有已发布文章
    $current_date = current_time('Y-m-d');
    $last_week_date = date('Y-m-d', strtotime('-1 week', strtotime($current_date)));

    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'date_query' => array(
            'after' => $last_week_date,
            'before' => $current_date,
            'inclusive' => true
        )
    );

    $posts = get_posts($args);

    // 循环遍历文章并删除它们
    foreach ($posts as $post) {
        wp_delete_post($post->ID, true);
    }
}

// 安排每周运行事件
if (!wp_next_scheduled('delete_custom_post_type_posts_event')) {
    wp_schedule_event(time(), 'weekly', 'delete_custom_post_type_posts_event');
}

// 将函数挂钩到预定事件
add_action('delete_custom_post_type_posts_event', 'delete_custom_post_type_posts');
英文:

You can delete published posts every week by setting up a wp_schedule_event

Hope this code helps

// Define the custom post type that you want to delete automatically
$custom_post_type = 'your_custom_post_type';

// Function to delete the custom post type posts
function delete_custom_post_type_posts() {
	// Get all published posts of the custom post type
	$current_date = current_time('Y-m-d');
	$last_week_date = date('Y-m-d', strtotime('-1 week', strtotime($current_date)));
	
	$args = array(
		'post_type' => 'post',
		'post_status' => 'publish',
		'posts_per_page' => -1,
		'date_query' => array(
			'after' => $last_week_date,
			'before' => $current_date,
			'inclusive' => true
		)
	);
	
	$posts = get_posts( $args );
	
	// Loop through the posts and delete them
	foreach ( $posts as $post ) {
		wp_delete_post( $post->ID, true );
	}
}

// Schedule the event to run every week
if ( ! wp_next_scheduled( 'delete_custom_post_type_posts_event' ) ) {
	wp_schedule_event( time(), 'weekly', 'delete_custom_post_type_posts_event' );
}

// Hook the function to the scheduled event
add_action( 'delete_custom_post_type_posts_event', 'delete_custom_post_type_posts' );

答案2

得分: 0

我认为这段代码将满足您的要求。

// 定义要自动删除的自定义文章类型
$custom_post_type = 'your_custom_post_type';

// 删除自定义文章类型文章的函数
function delete_custom_post_type_posts() {
    // 获取自定义文章类型的所有已发布文章
    $current_date = current_time('Y-m-d');
    $yesterday_date = date('Y-m-d', strtotime('-1 day', strtotime($current_date)));

    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'date_query' => array(
            'after' => $yesterday_date,
            'before' => $current_date,
            'inclusive' => true
        )
    );

    $my_posts = get_posts($args);

    // 遍历文章并删除它们
    foreach ($my_posts as $my_post) {
        wp_delete_post($my_post->ID, true);
    }
}

// 安排事件每周运行一次
if (!wp_next_scheduled('delete_custom_post_type_posts_event')) {
    wp_schedule_event(time(), 'twicedaily', 'delete_custom_post_type_posts_event');
}

// 将函数挂钩到计划事件
add_action('delete_custom_post_type_posts_event', 'delete_custom_post_type_posts');
英文:

I think, this code will full fill your requirements.

// Define the custom post type that you want to delete automatically
$custom_post_type = 'your_custom_post_type';

// Function to delete the custom post type posts
function delete_custom_post_type_posts() {
	// Get all published posts of the custom post type
	$current_date = current_time('Y-m-d');
	$yesterday_date = date('Y-m-d', strtotime('-1 day', strtotime($current_date)));
	
	$args = array(
		'post_type' => 'post',
		'post_status' => 'publish',
		'posts_per_page' => -1,
		'date_query' => array(
			'after' => $yesterday_date,
			'before' => $current_date,
			'inclusive' => true
		)
	);
	
	$my_posts = get_posts( $args );
	
	// Loop through the posts and delete them
	foreach ( $my_posts as $my_post ) {
		wp_delete_post( $my_post->ID, true );
	}
}

// Schedule the event to run every week
if ( ! wp_next_scheduled( 'delete_custom_post_type_posts_event' ) ) {
	wp_schedule_event( time(), 'twicedaily', 'delete_custom_post_type_posts_event' );
}

// Hook the function to the scheduled event
add_action( 'delete_custom_post_type_posts_event', 'delete_custom_post_type_posts' );```

</details>



huangapple
  • 本文由 发表于 2023年2月10日 03:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定