skip to Main Content

I have created a custom post similar to WordPress posts that I want to delete automatically every time (for example, every day or every week).
Is there a function for this?
I know that you can delete trash posts with the following function

define('EMPTY_TRASH_DAYS', 10 );

But what about custom created posts?

Thanks for your help

2

Answers


  1. You can delete published posts every week by setting up a wp_schedule_event

    Hope this code helps

    // Define the custom post type that you want to delete automatically
    $custom_post_type = 'your_custom_post_type';
    
    // Function to delete the custom post type posts
    function delete_custom_post_type_posts() {
        // Get all published posts of the custom post type
        $current_date = current_time('Y-m-d');
        $last_week_date = date('Y-m-d', strtotime('-1 week', strtotime($current_date)));
        
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'date_query' => array(
                'after' => $last_week_date,
                'before' => $current_date,
                'inclusive' => true
            )
        );
        
        $posts = get_posts( $args );
        
        // Loop through the posts and delete them
        foreach ( $posts as $post ) {
            wp_delete_post( $post->ID, true );
        }
    }
    
    // Schedule the event to run every week
    if ( ! wp_next_scheduled( 'delete_custom_post_type_posts_event' ) ) {
        wp_schedule_event( time(), 'weekly', 'delete_custom_post_type_posts_event' );
    }
    
    // Hook the function to the scheduled event
    add_action( 'delete_custom_post_type_posts_event', 'delete_custom_post_type_posts' );
    
    Login or Signup to reply.
  2. I think, this code will full fill your requirements.

    // Define the custom post type that you want to delete automatically
    $custom_post_type = 'your_custom_post_type';
    
    // Function to delete the custom post type posts
    function delete_custom_post_type_posts() {
        // Get all published posts of the custom post type
        $current_date = current_time('Y-m-d');
        $yesterday_date = date('Y-m-d', strtotime('-1 day', strtotime($current_date)));
        
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'date_query' => array(
                'after' => $yesterday_date,
                'before' => $current_date,
                'inclusive' => true
            )
        );
        
        $my_posts = get_posts( $args );
        
        // Loop through the posts and delete them
        foreach ( $my_posts as $my_post ) {
            wp_delete_post( $my_post->ID, true );
        }
    }
    
    // Schedule the event to run every week
    if ( ! wp_next_scheduled( 'delete_custom_post_type_posts_event' ) ) {
        wp_schedule_event( time(), 'twicedaily', 'delete_custom_post_type_posts_event' );
    }
    
    // Hook the function to the scheduled event
    add_action( 'delete_custom_post_type_posts_event', 'delete_custom_post_type_posts' );```
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search