Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save serkanalgur/f4f307ecb0b733648b823bb67a31806c to your computer and use it in GitHub Desktop.
Save serkanalgur/f4f307ecb0b733648b823bb67a31806c to your computer and use it in GitHub Desktop.
<?php
/**
* Yazı Adı : Geçmiş Yazıları ve Ortam Dosyalarını Otomatik Silmek
* Yazı Linki : https://wpadami.com/cms-sistemleri/wordpress/gecmis-yazilari-ortam-dosyalarini-otomatik-silmek.html.
**/
// Kodu lütfen dikkatli kullanın. Saygılarımla, Serkan Algur :)
// Kod başlasın
function delete_oldest_posts_salgur()
{
// İki yıldan eski yazıları toplayalım :)
$args = [
'date_query' => [
[
'column' => 'post_date_gmt',
'before' => '2 years ago', // isteğinize göre süreyi değiştirin
],
],
'posts_per_page' => -1,
];
// WP_Query ile yazıları toplayalım
$query = new WP_Query($args);
// Aşağıdakini 'foreach' kullanabilmek için yapıyoruz
$posts = $query->get_posts();
foreach ($posts as $post) {
echo $post->ID;
$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'post_mime_type' => 'image', // Ortamlardan resmi seçtik. Daha fazla opsiyon için https://codex.wordpress.org/Function_Reference/get_children
'post_parent' => $post->ID,
'post_type' => 'attachment',
];
$attachments = get_children($args);
if ($attachments) {
foreach ($attachments as $attachment) {
wp_delete_attachment($attachment->ID, true); //If You Want to trash post set true to false
}
}
wp_delete_post($post->ID, true); //If You Want to trash post set true to false
}
}
// Problemi çözecek olan cron oluşturucusu
function cron_delete_oldest_posts_salgur()
{
if (!wp_next_scheduled('delete_oldest_posts_salgur')) {
wp_schedule_event(current_time('timestamp'), 'daily', 'delete_oldest_posts_salgur');
}
}
add_action('init', 'cron_delete_oldest_posts_salgur');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment