0

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' );
    }
} 

UPDATE

Hello and I am very grateful for help me guys.

Cron event is active. Also I change the post status from expire to publish and I remove the 'key' => '' Now the problem is that if I run this “updated function” it move all posts to trash! AND it DOES NOT move the post(s) that are in 2 days old .

for example I have two posts: event1 and event2. the post with name “event1” is created at “05-12-2020 ” and the post with name “event2” is created at “08-12-2020 ”. If the function is set with $past = strtotime( "- 2 days " ); then the function must move the post with name “event1” in to trash.

Any ideas;

function get_exired_posts_to_delete()
{

    $past = strtotime( "- 2 days " );

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

3
  • How ae you debugging this? does Cron run, does it find your action - does the get_posts return any results - you need to locate the point at which things breaks down in order to resolve any problems.
    – Q Studio
    Commented Dec 8, 2020 at 11:10
  • Why is your meta key blank? I see your post status is set to expired, that isn't a valid post status in standard WP, how is that being set? Have you confirmed your cron job runs?
    – Tom J Nowell
    Commented Dec 8, 2020 at 12:10
  • i have update my "question" !
    – Lefteris
    Commented Dec 8, 2020 at 17:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.