admin管理员组文章数量:1122846
I wanted to turn any of my published posts into drafts after X days. I tried doing it with multiple WordPress plugins, but they were no help. Can anyone help me do it? Thank you
I wanted to turn any of my published posts into drafts after X days. I tried doing it with multiple WordPress plugins, but they were no help. Can anyone help me do it? Thank you
Share Improve this question asked Apr 27, 2021 at 11:15 ACEACE 111 bronze badge 02 Answers
Reset to default 0I would suggest you use wp_schedule_event()
and create a custom hook that you trigger daily. This hook can then call a custom function in which you query posts with the desired publish date, and change their status.
For example, create a new plugin and add (note: I have not tested this):
// Schedule auto-drafting post event on plugin activation.
register_activation_hook( __FILE__, 'schedule_auto_draft_posts' );
add_action( 'auto_draft_posts', 'draft_posts' );
function schedule_auto_draft_posts() {
wp_schedule_event( time(), 'daily', 'auto_draft_posts' );
}
// Draft published posts based on the post's publish date.
function draft_posts() {
$old_status = 'publish';
$new_status = 'draft';
// Example: get published posts made before last week.
$posts = get_posts(
array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'before' => '1 week ago',
),
),
'fields' => 'all',
'numberposts' => -1,
'post_status' => $old_status,
)
);
foreach ($posts as $post) {
// Update the post status.
wp_update_post(
'ID' => $post->ID,
'status' => $new_status,
);
// Fire actions related to the transitioning of the post's status.
wp_transition_post_status( $new_status, $old_status, $post );
}
}
// Clean the scheduler on plugin deactivation.
register_deactivation_hook( __FILE__, 'unschedule_auto_draft_posts' );
function unschedule_auto_draft_posts() {
wp_clear_scheduled_hook( 'auto_draft_posts' );
}
Documentation:
- Understanding WP-Cron Scheduling
- If you would like to retrieve posts with your own date query, you could use PHP's relative date formats.
Documentation of the functions used:
register_activation_hook()
add_action()
wp_schedule_event()
get_posts()
wp_update_post()
wp_transition_post_status()
register_deactivation_hook()
wp_clear_scheduled_hook()
just two little comment to Cas Dekkers code.
- At
wp_update_post(
'ID' => $post->ID,
'post_status' => $new_status,
);
it's should be an array:
wp_update_post(array(
'ID' => $post->ID,
'post_status' => $new_status,
);
);
or
$my_post = array(
'ID' => $post->ID,
'post_status' => $new_status,
);
wp_update_post($my_post);
- It can be set a custom time with using filters:
add_filter( 'cron_schedules', 'my_own_time_schedule' );
function my_own_time_schedule( $schedules ) {
$schedules['one_minut'] = array(
'interval' => 60, //seconds
'display' => __( 'Once Minut' )
);
$schedules['ten_minutes'] = array(
'interval' => 600,
'display' => __( 'Every Ten Minutes' )
);
return $schedules;
}
本文标签: How to draft posts after being published with x days
版权声明:本文标题:How to draft posts after being published with x days 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281962a1926491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论