function post to trash problem

Question

I know that are some questions like mine BUT I did not find solution for my problem!

I try to create for client an auto post expire for custom post type called “event” that when a new post is created, then after a some time ( after 30 minutes or 2 days) the post must to go to trash.

I found a code in this post “ Delete expired posts after a number of days after they expired

But does not work on me.

Any help is appreciated

Here is the code that I use and not working :

function get_exired_posts_to_delete()
{
    
    $past = strtotime( "- 30 minute" );

    // Set our query arguments
    $args = [
        'fields'         => 'ids', // Only get post ID's to improve performance
        'post_type'      => 'event',
        'post_status'    => 'expired',
        'posts_per_page' => -1,
        'meta_query'     => [
            [
                'key'     => '',
                'value'   => $past,
                '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( 'expired_post_delete', 'get_exired_posts_to_delete' );

// Add function to register event to wp
add_action( 'wp', 'register_daily_post_delete_event');
function register_daily_post_delete_event() {
    // Make sure this event hasn't been scheduled
    if( !wp_next_scheduled( 'expired_post_delete' ) ) {
        // Schedule the event
        wp_schedule_event( time(), 'daily', 'expired_post_delete' );
    }
} 

0
Lefteris 8 months 2020-12-08T04:10:18-05:00 0 Answers 4 views 0

Leave an answer

Browse
Browse