Wordpress: Automatically Delete Posts That Are X Days Old

Wordpress: automatically delete posts that are x days old?

Here's some SQL that will find every post that has been around for 30 days or more:

SELECT * FROM `wp_posts`
WHERE `post_type` = 'post'
AND DATEDIFF(NOW(), `post_date`) > 30

To delete all the posts, you can replace SELECT * FROM with DELETE FROM — but make sure you take a backup before you do this!

You can then just cron that however you like, be that a shell script, a PHP script, or whatever you're most comfortable with.

Auto delete published posts after a specific number of days in wordpress

Are you sure you need WordPress for this? It seems overly complicated to force WordPress into a continuing process like that.

I would suggest just putting the following SQL statement in a cron job (like a single .php file), and the rest will be handled by itself.

DELETE FROM wp_posts WHERE post_date < DATE_SUB(NOW(), INTERVAL 30 DAY);

Here's a good rundown on using cron to automatically do these kinds of this for you

Update

Using the WordPress scheduling function we can provide a cron-like time-based scheduling of our post removal. This isn't a true cron job as it removes our posts only after a person has visited the site after 30 days, and not after 30 days regardless of visitors. However, it's the closest we'll get via WordPress and in this case the results would be the same. Add the following to functions.php or your plugin file.

/**
* Add monthly interval to the schedules (since WP doesnt provide it from the start)
*/
add_filter('cron_schedules','cron_add_monthly');
function cron_add_monthly($schedules) {
$schedules['monthly'] = array(
'interval' => 2419200,
'display' => __( 'Once per month' )
);
return $schedules;
}
/**
* Add the scheduling if it doesnt already exist
*/
add_action('wp','setup_schedule');
function setup_schedule() {
if (!wp_next_scheduled('monthly_pruning') ) {
wp_schedule_event( time(), 'monthly', 'monthly_pruning');
}
}
/**
* Add the function that takes care of removing all rows with post_type=post that are older than 30 days
*/
add_action( 'monthly_pruning', 'remove_old_posts' );
function remove_old_posts() {
global $wpdb;
$wpdb->query($wpdb->prepare("DELETE FROM wp_posts WHERE post_type='post' AND post_date < DATE_SUB(NOW(), INTERVAL 30 DAY);"));
}

Delete posts older than 2 years in wordpress site

Try with this code.. Please read my comments in the code for more understanding. Function will go into your functions.php

function get_delete_old_post() {
// WP_Query arguments
$args = array(
'fields' => 'ids', // Only get post ID's to improve performance
'post_type' => array( 'post' ), //post type if you are using default than it will be post
'posts_per_page' => '-1',//fetch all posts,
'date_query' => array(
'column' => 'post_date',
'before' => '-2 years'
)//date query for before 2 years you can set date as well here
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
//delete post code
//wp_trash_post( get_the_ID() ); use this function if you have custom post type
wp_delete_post(get_the_ID(),true); //use this function if you are working with default posts
}
} else {
// no posts found
return false;

}
die();
// Restore original Post Data
wp_reset_postdata();

}

add_action('init','get_delete_old_post');

Auto delete WordPress images/thumbnails (all sizes) after X days/hours, or similar?

http://wordpress.org/plugins/cleanup-images/

or in code add hook with delete post

function delete_post_media( $post_id ) {

$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $post_id
) );

foreach ( $attachments as $attachment ) {
if ( false === wp_delete_attachment( $attachment->ID ) ) {
// Log failure to delete attachment.
}
}
}

try this

hope it will use full for you.

WordPress: Delete posts by using the date of a custom field

I found a solution

Here is my code:

function get_delete_old_events() {

$past_query = date('Y-m-d', strtotime('-1 day'));

// Set our query arguments
$args = [
'fields' => 'ids', // Only get post ID's to improve performance
'post_type' => 'event', // Post type
'posts_per_page' => -1,
'meta_query' => [
[
'key' => 'gid_22', // Replace this with the event end date meta key.
'value' => $past_query,
'compare' => '<='
]
]
];
$q = get_posts( $args );

// Check if we have posts to delete, if not, return false
if ( !$q )
return false;

// OK, we have posts to delete, lets delete them
foreach ( $q as $id )
wp_trash_post( $id );
}

// expired_post_delete hook fires when the Cron is executed
add_action( 'old_event_delete', 'get_delete_old_events' );

// Add function to register event to wp
add_action( 'wp', 'register_daily_events_delete_event');

function register_daily_events_delete_event() {
// Make sure this event hasn't been scheduled
if( !wp_next_scheduled( 'old_event_delete' ) ) {
// Schedule the event
wp_schedule_event( time(), 'hourly', 'old_event_delete' );
}
}

I've changed the argument wp_delete_post() to wp_trash_post() because wp_delete_post() only applies to native posts, pages, and attachments. Great answer from @rarst here: https://wordpress.stackexchange.com/questions/281877/error-after-deleting-custom-post-type-with-a-function-no-trash-used/281888#281888



Related Topics



Leave a reply



Submit