WordPress: Delete posts by using the date of a custom field WordPress: Delete posts by using the date of a custom field wordpress wordpress

WordPress: Delete posts by using the date of a custom field


I found a solution

Here is my code:

function get_delete_old_events() {    $past_query = date('Y-m-d', strtotime('-1 day'));    // Set our query arguments    $args = [        'fields'         => 'ids', // Only get post ID's to improve performance        'post_type'      => 'event', // Post type        'posts_per_page' => -1,        'meta_query'     => [            [                'key'     => 'gid_22', // Replace this with the event end date meta key.                'value'   => $past_query,                '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 executedadd_action( 'old_event_delete', 'get_delete_old_events' );// Add function to register event to wpadd_action( 'wp', 'register_daily_events_delete_event');function register_daily_events_delete_event() {    // Make sure this event hasn't been scheduled    if( !wp_next_scheduled( 'old_event_delete' ) ) {        // Schedule the event        wp_schedule_event( time(), 'hourly', 'old_event_delete' );    }}

I've changed the argument wp_delete_post() to wp_trash_post() because wp_delete_post() only applies to native posts, pages, and attachments. Great answer from @rarst here: https://wordpress.stackexchange.com/questions/281877/error-after-deleting-custom-post-type-with-a-function-no-trash-used/281888#281888


The link you provided shows you how to do this, you simply need to change some of the information in the answer to get it to work.

function expirePastEvents() {    $postType = 'events'; // Change this to your post type name.    $metaKeyName = 'end_date'; // Change this to your meta key name for end date.    $skipTrash = false; // Whether or not to skip the trash.    $posts = new WP_Query([        'post_type' => $postType,        'fields' => 'ids',        'post_status' => 'publish',        'meta_query' => [            [                'key' => $metaKeyName,                'value' => current_time('timestamp'),                'compare' => '<='            ]        ]    ]);    foreach ($posts->posts as $post) {        wp_delete_post($post->ID, $skipTrash);    }}


Do the following steps:

  1. Install WP Control Plugin.
  2. Activate the plugin.
  3. Navigate to wp-admin/tools.php?page=crontrol_admin_manage_page and in Add Cron Event section add 'my_daily_event_delete' in the Hook Name tabs below.
  4. Save the event.

    Add the below code in the your functions.php file.

    add_action('my_daily_event_delete', '_delete_this_daily');function _delete_this_daily() {  $past = strtotime( "- 1 day" );  // Set our query arguments  $args = [    'fields'         => 'ids', // Only get post ID's to improve performance    'post_type'      => 'event', // Post type    'posts_per_page' => -1,    'meta_query'     => [        [            'key'     => 'event_end_date', // Replace this with the event end date meta 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_delete_post( $id );  }}