1

RESOLVED ## updated code below.

I had created custom post type called 'esitykset' and I wanted to delete them if they are older that the date in 'show_date' custom field of the post.

So I tried the following code

 add_action( 'wp', 'expire_events_daily' );
function expire_events_daily() {
    if ( ! wp_next_scheduled( 'delete_expired_events' ) ) {
        wp_schedule_event( time(), 'hourly', 'delete_expired_events');
    }
}
add_action( 'delete_expired_events', 'delete_expired_events_callback' );


function delete_expired_events_callback() 
{
    $args = [
        'post_type'      => 'esitykset',
        'posts_per_page' => -1,
        'fields'         => 'ids', //only get post id's
        'meta_query'     => [
            [
               'key'     => 'show_date',
               'value'   => current_time( 'Y/m/d' ),
               'compare' => '<'
            ]
        ]
    ];

    $events = get_posts( $args );

    if ( $events ) {
        // Loop through the post ID's and delete the post
        foreach ( $events as $id )
            wp_trash_post( $id );
    }
}

For the code above to work, I had to add

define('ALTERNATE_WP_CRON', true);

in my wp-config.php file.

Now it worked. Thanks for helping out.

2
  • What is the format in which the date is saved in your custom field. Also, time() returns a unix timestamp. If your dates in your custom field is not saved as unix timestamp, thing will never work. Your date/time formats should be in the same exact format. Also note, for this to work, if your dates are not saved in unix, the format have to be year month day, any other format will not work Commented May 11, 2016 at 11:50
  • Hello Pieter, the date is saved in YYYY/MM/DD format. I believe it is a Unix timestamp?
    – Mayank
    Commented May 11, 2016 at 12:24

1 Answer 1

0

You have a date format mismatch here. You say your dates are saved as Y/m/d in your custom field, which is correct, but then you try to match this format to unix timestamp (which is the result from time()).

Date and time formats need to be in exactly the same format in order to match or to compare two different dates in a custom field, this is how custom fields work. There is no logic to know and no logic to convert and match different date/time formats, so you will need to manually make sure that everything matches up.

Instead of using time() to return the current date/time and using that as date/time comparison, you should use current_time( 'Y/m/d' ). Just a note, this is only possible as from WordPress 3.9

Just another note, we can simplify your function as follow, which will be faster and better for performance (NOTE: Requires PHP 5.4 at least)

function delete_expired_events_callback() 
{
    $args = [
        'post_type'      => 'esitykset',
        'posts_per_page' => -1,
        'fields'         => 'ids', //only get post id's
        'meta_query'     => [
            [
               'key'     => 'show_date',
               'value'   => current_time( 'Y/m/d' ),
               'compare' => '<'
            ]
        ]
    ];

    $events = get_posts( $args );

    if ( $events ) {
        // Loop through the post ID's and delete the post
        foreach ( $events as $id )
            wp_delete_post( $id );
    }
}
4
  • Hi Pieter, thanks but this code. I simply copied and pasted it and still didn't work. I am wondering if i wp_schedule_eventis working at all. Do you knwo if it could be some other problem?
    – Mayank
    Commented May 12, 2016 at 9:39
  • Hi Pieter, I'd really appreciate if you could help me with this. I have updated the code I am running my child theme's functions.php
    – Mayank
    Commented May 13, 2016 at 10:30
  • Have you seen this. Also, make 100% sure that your formats for your dates are matching up Commented May 13, 2016 at 10:56
  • Thanks Pieter. You were right, I added define('ALTERNATE_WP_CRON', true); to my wp-config.php file and it worked. I am updating the code above.
    – Mayank
    Commented May 16, 2016 at 12:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.