WordPress:使用自定义字段的日期删除帖子 [英] WordPress: Delete posts by using the date of a custom field

查看:109
本文介绍了WordPress:使用自定义字段的日期删除帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a Custom Post Type to show events on my site. The events have two custom fields for the start and the end date. The dates are stored in the following format: YYYY-MM-DD.

我正在使用该字段对前端中发生下一个事件的事件进行排序基于当前日期。

I'm using the field to sort the events in the frontend where I start with the next event based on the current date.

在此日期之后,事件不再位于前端,因为它们位于过去。

After this date, the events are not showing in the frontend anymore because they lie in the past.

现在,我想删除结束日期之后的事件。
有什么办法吗?

Now I want to delete the events after the end date. Is there any way to do this?

我从@ pieter-goosen找到了一个不错的解决方案,可以在几天后删除帖子: https://wordpress.stackexchange.com/问题/ 209046 /删除过期后的天数后的帖子

I've found a nice solution from @pieter-goosen to delete posts after a number of days: https://wordpress.stackexchange.com/questions/209046/delete-expired-posts-after-a-number-of-days-after-they-expired

但我不知道

任何想法/提示?

我的代码:

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


推荐答案

I找到了解决方案

这是我的代码:

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

我更改了参数 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

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

这篇关于WordPress:使用自定义字段的日期删除帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆