Hướng thực hiện
- Viết truy vấn lọc tất cả bài viết trước đây 1 tháng
- Update trang thái của bài viết sử dụng hàm: wp_update_post
- Lên lịch tư động thực hiện chạy hàm ở bước thứ 2 ở trên
Bắt đầu
1. Truy vấn lọc tất cả bài viết trước đây 1 tháng
$args = array( 'posts_per_page' => -1, 'post_type' => 'post', // page, your custom post type 'date_query' => array( 'column' => 'post_date', // or post_date_gmt múi giờ chuẩn 'before' => '1 month ago' ), ); $query = new WP_Query( $args );
2. Phương thức update trạng thái của bài viết
add_action( 'wp_footer', 'maodev_update_status_draf_all_post_before_one_month_ago' );
function maodev_update_status_draf_all_post_before_one_month_ago(){
$start_time = microtime( true );
$args = array(
'posts_per_page' => -1,
'post_type' => 'post', // page, your custom post type
'date_query' => array(
'column' => 'post_date', // or post_date_gmt múi giờ chuẩn
'before' => '1 month ago'
),
);
$query = new WP_Query( $args );
// The Loop
$posts_update_error = array();
if ( $query->have_posts() ) {
echo "Tổng số bài: $query->post_count đã được tìm thấy <br>";
$temp = 0;
while ( $query->have_posts() ) {
$query->the_post();
// update status
$postarr = array(
'ID' => get_the_ID(),
'post_status' => 'pending',
);
$post_id = wp_update_post( $postarr );
if( !is_wp_error( $post_id ) ){
$temp++;
}else{
$posts_update_error[] = get_the_ID();
}
}
echo "Tổng số $temp bài đã được update <br>";
}
if( !empty( $posts_update_error ) ){
$post_error_str = implode(', ', $posts_update_error);
echo "Những ID bài viết update lỗi: $post_error_str <br>";
}
/* Restore original Post Data */
wp_reset_postdata();
$end_time = microtime( true );
$time_update = $end_time - $start_time;
echo "Tổng số thời gian thự thi $query->post_count bài viết là: $time_update s <br>";
}
3. Lên lịch tự động dựa trên phương thức đã viết sẵn ở bước 2
// Hook into that action that'll fire every three minutes
if ( ! wp_next_scheduled( 'maodev_daily_event' ) ) {
wp_schedule_event( strtotime( '0am tomorrow' ), 'daily', 'maodev_daily_event' );
}
add_action( 'maodev_daily_event', 'maodev_update_status_pending_all_post_before_one_month_ago' );
function maodev_update_status_pending_all_post_before_one_month_ago() {
$start_time = microtime( true );
$args = array(
'posts_per_page' => -1,
'post_type' => 'nha_dat', // page, your custom post type
'date_query' => array(
'column' => 'post_date', // or post_date_gmt múi giờ chuẩn
'before' => '1 month ago'
),
);
$query = new WP_Query( $args );
// The Loop
$result = '';
$posts_update_error = array();
if ( $query->have_posts() ) {
$result .= "Tổng số bài: $query->post_count đã được tìm thấy <br>";
$temp = 0;
while ( $query->have_posts() ) {
$query->the_post();
// update status
$postarr = array(
'ID' => get_the_ID(),
'post_status' => 'pending',
);
$post_id = wp_update_post( $postarr );
if( !is_wp_error( $post_id ) ){
$temp++;
}else{
$posts_update_error[] = get_the_ID();
}
}
$result .= "Tổng số $temp bài đã được update <br>";
}
if( !empty( $posts_update_error ) ){
$post_error_str = implode(', ', $posts_update_error);
$result .= "Những ID bài viết update lỗi: $post_error_str <br>";
}
/* Restore original Post Data */
wp_reset_postdata();
$end_time = microtime( true );
$time_update = $end_time - $start_time;
$result .= "Tổng số thời gian thự thi $query->post_count bài viết là: $time_update s <br>";
file_put_contents('log_sch_event_post_pending.txt', $result ."\n====\n" , FILE_APPEND );
}