Categories
CMS Development Wordpress

Move to trash custom post type post after ACF Date Picker date is older than today

I have a custom post type called ‘aktualitates’ and I have a date picker field on the posts from the Advanced Custom Fields plugin with the field name ‘aktuali_expire’. I tried adding a code from another question to functions.php, but it doesn’t seem to work for me. I am positive, that the time values are both set to Y/m/d in this code and in ACF field settings.

I also tried setting the action as scheduled, as shown in the code from this question, but after hours it didn’t seem to trigger..

add_action( 'init', 'delete_expired_events' );

function delete_expired_events() 
{

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

$aktualitates = get_posts( $args );

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

Any help will be appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *