WordPress:使用自定义字段的日期删除帖子

Cra*_*ray 5 php wordpress date custom-post-type

我正在使用自定义帖子类型来显示我网站上的事件。事件有两个自定义字段,分别用于开始日期和结束日期。日期按以下格式存储:YYYY-MM-DD。

我使用该字段对前端中的事件进行排序,在前端根据当前日期从下一个事件开始。

在此日期之后,事件将不再显示在前端,因为它们已成为过去。

现在我想删除结束日期之后的事件。有什么办法可以做到这一点吗?

我从 @pieter-goosen 找到了一个很好的解决方案,可以在几天后删除帖子:https://wordpress.stackexchange.com/questions/209046/delete-expired-posts-after-a-number-of-days -过期后

但我不知道如何将此函数与元字段一起使用。

有什么想法/提示吗?

我的代码:

function expirePastEvents() {

    $current_date_query = date ('Y-m-d');

    $postType = 'event'; // Change this to your post type name.
    $metaKeyName = 'gid_22'; // 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'),
                'value' => $current_date_query,
                'compare' => '<='
            ]
        ]
    ]);

    foreach ($posts->posts as $post) {
        wp_delete_post($post->ID, $skipTrash);
    }
}



// expired_post_delete hook fires when the Cron is executed
add_action( 'expired_post_delete', 'expirePastEvents' );


// Add function to register event to wp
add_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( 'expired_post_delete' ) ) {
        // Schedule the event
        wp_schedule_event( time(), 'hourly', 'expired_post_delete' );
    }
}
Run Code Online (Sandbox Code Playgroud)

Cra*_*ray 4

我找到了解决方案

这是我的代码:

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 executed
add_action( 'old_event_delete', 'get_delete_old_events' );

// Add function to register event to wp
add_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' );
    }
}
Run Code Online (Sandbox Code Playgroud)

我已将参数更改wp_delete_post()wp_trash_post()因为wp_delete_post()仅适用于本机帖子、页面和附件。@rarst 这里给出了很好的答案:https://wordpress.stackexchange.com/questions/281877/error-after-deleting-custom-post-type-with-a-function-no-trash-used/281888#281888