You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress single.php模板中每周执行一次价格更新PHP代码?

嘿,这个需求我太熟了!之前帮好几个朋友解决过类似的问题,给你捋个清晰的方案,把逻辑从single.php移到functions.php,同时实现每周只更新一次的效果:

核心思路:自己记录检查时间,别依赖文章修改日期

你说得对,更新文章元数据的时候,WordPress不会自动修改文章的modified日期,所以咱们得自己加个自定义字段(比如_last_price_check)来存上次检查价格的时间戳。每次要执行更新逻辑前,先算一下当前时间和这个时间戳的差值,超过7天再继续干活。

方案一:基于页面访问触发(适合访问量正常的网站)

把价格检查和更新的逻辑封装成函数,挂到页面加载的钩子上,每次用户打开文章页时,先判断时间条件,满足才执行核心更新操作。这样不会每次加载都跑外部请求,只有到时间了才触发:

function update_external_price_weekly() {
    // 只在单文章页面触发,不想限制的话可以删掉这行
    if ( ! is_single() ) {
        return;
    }

    global $post;
    $post_id = $post->ID;

    // 获取上次检查的时间戳,默认0代表从未检查过
    $last_check_time = get_post_meta( $post_id, '_last_price_check', true );
    $last_check_time = empty( $last_check_time ) ? 0 : (int)$last_check_time;

    // 计算一周的秒数:7天×24小时×60分钟×60秒 = 604800
    $one_week_in_seconds = 604800;
    $current_time = time();

    // 还没到一周,直接返回不执行
    if ( $current_time - $last_check_time < $one_week_in_seconds ) {
        return;
    }

    // ↓↓↓ 这里替换成你原来的价格获取逻辑 ↓↓↓
    $external_price = file_get_contents('https://example.com/your-price-api?id=' . $post_id);
    $external_price = floatval($external_price);

    // 获取当前存储的价格(替换成你的自定义字段名)
    $existing_price = get_post_meta( $post_id, 'current_price', true );
    $existing_price = empty( $existing_price ) ? 0 : floatval($existing_price);

    // 价格不一样就更新
    if ( $external_price !== $existing_price ) {
        update_post_meta( $post_id, 'current_price', $external_price );
        // 可选:如果需要手动更新文章修改日期,解开下面这行注释
        // wp_update_post( array( 'ID' => $post_id, 'post_modified' => current_time('mysql'), 'post_modified_gmt' => current_time('mysql', 1) ) );
    }
    // ↑↑↑ 你的原有逻辑结束 ↑↑↑

    // 更新上次检查时间为当前时间
    update_post_meta( $post_id, '_last_price_check', $current_time );
}
// 挂到页面加载钩子上,每次页面加载时触发判断
add_action( 'template_redirect', 'update_external_price_weekly' );
方案二:用WP-Cron定时执行(更可靠,适合低访问量网站)

如果你的网站访问量很低,可能一周都没人打开文章页,那上面的方案就没法触发更新。这时候用WordPress的定时任务(WP-Cron)更靠谱,每周自动执行一次,不管有没有人访问:

// 注册每周执行的定时任务
function register_weekly_price_check_cron() {
    // 检查是否已经注册了这个定时任务,避免重复注册
    if ( ! wp_next_scheduled( 'weekly_price_check_event' ) ) {
        // 第一次执行设为当前时间后10分钟,避免刚激活就跑
        wp_schedule_event( time() + 600, 'weekly', 'weekly_price_check_event' );
    }
}
add_action( 'wp', 'register_weekly_price_check_cron' );

// 定时任务要执行的核心逻辑
function run_weekly_price_check() {
    // 获取所有需要检查价格的文章,根据你的需求调整参数
    $args = array(
        'post_type' => 'post', // 替换成你的文章类型,比如'product'
        'posts_per_page' => -1, // 取所有发布状态的文章
        'post_status' => 'publish'
    );
    $posts = get_posts( $args );

    foreach ( $posts as $post ) {
        setup_postdata( $post );
        $post_id = $post->ID;

        // ↓↓↓ 同样替换成你的价格获取和更新逻辑 ↓↓↓
        $external_price = file_get_contents('https://example.com/your-price-api?id=' . $post_id);
        $external_price = floatval($external_price);

        $existing_price = get_post_meta( $post_id, 'current_price', true );
        $existing_price = empty( $existing_price ) ? 0 : floatval($existing_price);

        if ( $external_price !== $existing_price ) {
            update_post_meta( $post_id, 'current_price', $external_price );
        }
        // ↑↑↑ 你的逻辑结束 ↑↑↑

        // 更新上次检查时间
        update_post_meta( $post_id, '_last_price_check', time() );
    }
    wp_reset_postdata();
}
add_action( 'weekly_price_check_event', 'run_weekly_price_check' );
几个关键注意事项
  • 记得把代码里的current_price换成你实际用的价格自定义字段名,还有外部价格的API地址也要改成你自己的。
  • WP-Cron是依赖网站访问触发的,如果你的网站几乎没人访问,建议配置服务器的系统Cron(比如Linux的crontab),定时访问https://你的域名/wp-cron.php?doing_wp_cron,这样定时任务肯定能执行。
  • 如果获取外部价格需要复杂的请求(比如带请求头、SSL验证、代理),别用file_get_contents,换成curl会更稳定,避免请求失败。

内容的提问来源于stack exchange,提问作者Randy Hoffman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:31:56