Support » Plugins » Hacks » cron job to auto delete posts of a specific post type older than x days

  • Hello,

    I want to delete all posts of a specific post type (in this case “vfb_entry”) that are older than 60 days. The cron job should run once a day.

    I have the following code and would like to ask you guys if it is correct:

    // Cron Job to Delete VFB Entries older than 60 days
    if(!wp_next_scheduled( 'remove_old_vfb_entries')){
        wp_schedule_event(time(), 'daily', 'remove_old_vfb_entries');
    }
    add_action('remove_old_vfb_entries', 'myd_remove_old_vfb_entries');
    
    // Build the function
    function myd_remove_old_vfb_entries(){
    global $wpdb;
    // Set max post date and post_type name
    $date = date("Y-m-d H:i:s", strtotime('-60 days'));
    $post_type = 'vfb_entry';
    
    // Query post type
    $query = "
        SELECT ID FROM $wpdb->posts
        WHERE post_type = '$post_type'
        AND post_status = 'publish'
        AND post_author = '1'
        AND post_date < '$date'
        ORDER BY post_modified DESC
    ";
    $results = $wpdb->get_results($query);
                foreach($results as $post){
                    // Let the WordPress API clean up the entire post trails
                    wp_delete_post( $post->ID, true);
              }
    }

    Thank you!

Viewing 1 replies (of 1 total)
  • Hi
    I could be interested in something similar.

    Maybe you can help me. Did you find a solution?

    I look for a way to delete all the posts older than 30 days, automatically.

    And I would that it could work like the delete of every single buddypress post that remove everything including also the images (any data file) uploaded using rtmedia.

    If I go in the Actions Page and I delete a post the process remove also the rtmedia content uploaded and this is amazing.

    I would a script to use with wordpress or in server crontab to “activate” the delete process used do remove completely the buddypress posts.

    Can you help me?

    Kind regards

    G.Aloe

Viewing 1 replies (of 1 total)
  • The topic ‘cron job to auto delete posts of a specific post type older than x days’ is closed to new replies.