Last active
March 30, 2022 17:22
-
-
Save serkanalgur/d350ecebb3cb566c246d61c1a23d6213 to your computer and use it in GitHub Desktop.
Automatically Delete Posts older than 6 months ago
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Please use this function carefully. | |
// Changes can't undone. Best regards from Serkan Algur :) | |
// Let the function begin | |
function delete_oldest_posts_salgur( ) { | |
// We will collect posts from two years ago :) | |
$args = array( | |
'date_query' => array( | |
array( | |
'column' => 'post_date_gmt', | |
'before' => '6 months ago', | |
), | |
), | |
'posts_per_page' => -1, | |
'post_type' => 'memorials' | |
); | |
// Get posts via WP_Query | |
$query = new WP_Query( $args ); | |
//We are doing this for 'foreach' | |
$posts = $query->get_posts(); | |
foreach($posts as $post){ | |
echo $post->ID; | |
$args = array( | |
'posts_per_page' => -1, | |
'order' => 'ASC', | |
'post_mime_type' => 'image', // only for images. Look at 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 | |
} | |
} | |
// Problem Solver Cron Job definition | |
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( 'wp', 'cron_delete_oldest_posts_salgur' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Please use this function carefully. | |
// Changes can't undone. Best regards from Serkan Algur :) | |
// Let the function begin | |
function delete_oldest_posts_salgur( ) { | |
// We will collect posts from two years ago :) | |
$args = array( | |
'date_query' => array( | |
array( | |
'column' => 'post_date_gmt', | |
'before' => '2 years ago', | |
), | |
), | |
'posts_per_page' => -1, | |
'post_type' => 'memorials' | |
); | |
// Get posts via WP_Query | |
$query = new WP_Query( $args ); | |
//We are doing this for 'foreach' | |
$posts = $query->get_posts(); | |
foreach($posts as $post){ | |
echo $post->ID; | |
$args = array( | |
'posts_per_page' => -1, | |
'order' => 'ASC', | |
'post_mime_type' => 'image', // only for images. Look at 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 | |
} | |
} | |
/** | |
PS. The problem is my brain. Totally forgot to change the function name in the filter. Copy/Paste is not a good thing... (Also typo...) | |
**/ | |
add_filter( 'cron_schedules', 'cron_add_6months' ); | |
function cron_add_6months( $schedules ) { | |
// Adds Once six months to the existing schedules. | |
$schedules['6_monthly'] = array( | |
'interval' => 6 * MONTH_IN_SECONDS, | |
'display' => __( 'Once six months' ) | |
); | |
return $schedules; | |
} | |
// Problem Solver Cron Job definition | |
function cron_delete_oldest_posts_salgur() { | |
if ( ! wp_next_scheduled( 'delete_oldest_posts_salgur' ) ) { | |
wp_schedule_event( current_time( 'timestamp' ), '6_monthly', 'delete_oldest_posts_salgur' ); | |
} | |
} | |
add_action( 'wp', 'cron_delete_oldest_posts_salgur' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment