Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Untitled Snippet

// Scheduled Action Hook
function ht_delete_old_dailycake_dailyicecream( ) {
	global $wpdb;
	$days_to_close     = 31; // disable comments and ping backs on posts older than 31 days
	$competition_posts = 10; // mark competition posts as private after 10 days
	
	$wpdb->query(
		$wpdb->prepare(
			"UPDATE $wpdb->posts SET `comment_status` = %s, `ping_status` = %s WHERE `post_date_gmt` < DATE_SUB(%s, INTERVAL %d DAY);",
			array( 'closed', 'closed', current_time( 'mysql', 1 ), $days_to_close )
		)
	);
	
	$wpdb->query(
		$wpdb->prepare(
			"UPDATE $wpdb->posts SET `post_status` = %s WHERE `post_type` = %s AND `post_date_gmt` < DATE_SUB(%s, INTERVAL %d DAY);",
			array( 'private', 'competition', current_time( 'mysql', 1 ), $competition_posts )
		)
	);
}
add_action( 'ht_delete_old_dailycake_dailyicecream', 'ht_delete_old_dailycake_dailyicecream' );

// Custom Cron Recurrences
function disable_old_posts_comments_cron_job_recurrence( $schedules ) {
	$schedules['weekly'] = array(
		'display' => __( 'Once Weekly', 'textdomain' ),
		'interval' => 604800,
	);
	return $schedules;
}
add_filter( 'cron_schedules', 'disable_old_posts_comments_cron_job_recurrence' );

// Schedule Cron Job Event
function disable_old_posts_comments_cron_job() {
	if ( ! wp_next_scheduled( 'ht_delete_old_dailycake_dailyicecream' ) ) {
		wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'ht_delete_old_dailycake_dailyicecream' );
	}
}
add_action( 'wp', 'disable_old_posts_comments_cron_job' );