简体   繁体   English

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

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

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. 日期以以下格式存储: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? 有什么办法吗?

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 我发现@ pieter-goosen是一个不错的解决方案,可以在几天后删除帖子: https ://wordpress.stackexchange.com/questions/209046/delete-expired-posts-after-a-number-of-days -after-他们过期

But I don't know how to use this function with a meta field. 但是我不知道如何在元字段中使用此功能。

Any ideas/hints? 有什么想法/提示吗?

My code: 我的代码:

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

I've changed the argument wp_delete_post() to wp_trash_post() because wp_delete_post() only applies to native posts, pages, and attachments. 我将参数wp_delete_post()更改为wp_trash_post()因为wp_delete_post()仅适用于本机帖子,页面和附件。 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 来自@rarst的很好答案: 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. 安装WP控制插件。
  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. 导航至wp-admin / tools.php?page = crontrol_admin_manage_page,然后在“添加Cron事件”部分的“挂钩名称”标签中添加“ my_daily_event_delete”。
  4. Save the event. 保存事件。

    Add the below code in the your functions.php file. 将以下代码添加到您的functions.php文件中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM