Support » Fixing WordPress » Delete old posts programmatically

  • I want to delete old posts programmatically.
    I found smatterings of code but usually more complicated than I require.

    I put this together to delete posts older than 120 days.

    Will it work?

    require_once ‘wp-load.php’;

    global $wpdb;

    $expireds = $wpdb->get_results(“SELECT * FROM $wpdb->posts WHERE DAY(TIMEDIFF(NOW(), post_date_gmt)) >=120”);

    foreach ($expireds as $expired) {
    wp_delete_post($post->id, true);

  • The topic ‘Delete old posts programmatically’ is closed to new replies.