我使用自定义帖子类型来显示我网站上的活动。 事件有两个自定义字段用于开始和结束日期。日期以以下格式存储:YYYY-MM-DD。
我使用该字段对前端中的事件进行排序,我根据当前日期开始下一个事件。
在此日期之后,事件不再显示在前端,因为它们位于过去。
现在我想在结束日期之后删除事件。 有没有办法做到这一点?
我在@ pieter-goosen找到了一个很好的解决方案,可以在几天后删除帖子: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' );
}
}
答案 0 :(得分:1)
您提供的链接向您展示了如何执行此操作,您只需更改答案中的某些信息即可使其生效。
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);
}
}
答案 1 :(得分:1)
执行以下步骤:
保存活动。
在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 );
}
}
答案 2 :(得分:1)
我找到了解决方案
这是我的代码:
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