Wordpress教程 2024年09月22日
0 收藏 0 点赞 145 浏览 2191 个字

//二开自动更新文章日期为当天发布。
//设置宝塔面板中的定时任务,通过访问带有?update_posts=run的链接触发文章更新.如:curl -s https://xxx.com/?update_posts=run
//或者手动访问 域名/?update_posts=run 执行更新命令。


// 版本1  将文章的日期更新为当天的完整时间(年月日、然鹅小时、分钟均为之前的时间不变),从而达到重新发布文章的效果。
// function custom_manual_update_old_posts() {
//     if (isset($_GET['update_posts']) && $_GET['update_posts'] === 'run') {
//         $posts_per_page = 100; // 每次处理 100 篇文章
//         $paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1; 

//         $args = array(
//             'posts_per_page' => $posts_per_page,
//             'post_type' => 'post',
//             'post_status' => 'publish',
//             'orderby' => 'date',
//             'order' => 'ASC',
//             'paged' => $paged 
//         );

//         $old_posts = new WP_Query($args);

//         if ($old_posts->have_posts()) {
//             while ($old_posts->have_posts()) {
//                 $old_posts->the_post();
//                 $post_id = get_the_ID();

//                 $original_time = get_the_date('H:i:s', $post_id);
//                 $current_date = current_time('Y-m-d');
//                 $new_post_date = $current_date . ' ' . $original_time;

//                 $post_data = array(
//                     'ID'           => $post_id,
//                     'post_date'    => $new_post_date,
//                     'post_date_gmt'=> get_gmt_from_date($new_post_date),
//                 );
//                 wp_update_post($post_data);
//             }
//             wp_reset_postdata();
//         }

//         if ($old_posts->max_num_pages > $paged) {
//             $next_page_url = add_query_arg('paged', $paged + 1, $_SERVER['REQUEST_URI']);
//             wp_redirect($next_page_url);
//             exit;
//         }

//         echo '所有文章更新成功';
//         exit;
//     }
// }
// add_action('init', 'custom_manual_update_old_posts');



// 版本2----将文章的日期更新为当天的完整时间(年月日、小时、分钟均为当前时间),从而达到重新发布文章的效果。

function custom_manual_update_all_posts() {
    if (isset($_GET['update_posts']) && $_GET['update_posts'] === 'run') {
        $posts_per_page = 100; // 每次处理 100 篇文章
        $paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1; 

        $args = array(
            'posts_per_page' => $posts_per_page,
            'post_type' => 'post',
            'post_status' => 'publish',
            'orderby' => 'date',
            'order' => 'ASC',
            'paged' => $paged
        );

        $old_posts = new WP_Query($args);

        if ($old_posts->have_posts()) {
            while ($old_posts->have_posts()) {
                $old_posts->the_post();
                $post_id = get_the_ID();
                $current_time = current_time('mysql'); 
                $post_data = array(
                    'ID'           => $post_id,
                    'post_date'    => $current_time,
                    'post_date_gmt'=> get_gmt_from_date($current_time),
                );
                wp_update_post($post_data);
            }
            wp_reset_postdata();
        }

        if ($old_posts->max_num_pages > $paged) {
            $next_page_url = add_query_arg('paged', $paged + 1, $_SERVER['REQUEST_URI']);
            wp_redirect($next_page_url);
            exit;
        }

        echo '所有文章更新成功';
        exit;
    }
}
add_action('init', 'custom_manual_update_all_posts');

 

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.mizhanw.com/blog/4957.html

相关推荐
WordPress去除/禁止后台侧栏插件-主题-版本更新提示
今天使用了张戈修改的nginx-help插件修改版,官方版并无自动重启功能,因此不能更新。但是这WordPress后台一直有更新提示,这可真…
日期:2024-12-30 点赞:0 阅读:44
WordPress网站js防扒代码-禁止右键/F12/调试自动关闭窗口
网站防扒老生常谈了,今天老白博客@老白也给大家分享一下从网上看到的“WordPress网站防扒代码/禁止F12调试教程-以7b2主题为例” …
日期:2024-12-30 点赞:0 阅读:59
WordPress优化加速之导航菜单栏缓存Menu Cache
今天老白博客@老白给大家分享又一个WordPress优化加速教程:“导航菜单栏缓存WordPress插件Menu Cache”。我们都知道数…
日期:2024-12-30 点赞:0 阅读:45
2024基于网站性能测试的WordPress优化指南
老白博客WordPress优化高级教程:在如今5G遍地走,千兆多如狗的高速网络时代,如果你访问网站,还出现转圈圈的情况,那么你十有八九是要关…
日期:2024-12-30 点赞:0 阅读:50
WP网站用户行为监测回放插件nicen-replay
在数字营销的世界里,了解用户行为是提升用户体验和转化率的关键。老白推荐一款炫酷的WordPress插件:nicen-replay 。它能够让…
日期:2024-12-30 点赞:0 阅读:48
2024新WordPress缓存插件atec Cache APCu
WordPress插件atec Cache APCu 是一款2024年才投入使用的的高效缓存插件,与其他内存缓存选项(如 Redis 和 M…
日期:2024-12-30 点赞:0 阅读:65
发表评论
暂无评论

还没有评论呢,快来抢沙发~