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

修改WordPress函数实现每次10次迭代,增强更新处理能力

Modifying the WordPress Function for 10 Iterations per Execution

Got it, let's adjust your cron_revise_changed_listings function to ensure it runs exactly 10 iterations (processing up to 10 changed listings) every time it's triggered. Here's the updated code with clear explanations:

add_action( 'wp_loaded', 'cron_revise_changed_listings' );
function cron_revise_changed_listings() {
    global $wpdb;

    // Only execute if the trigger parameter is present
    if ( ! isset( $_REQUEST['wplister_revise_all'] ) ) {
        return;
    }

    // Set fixed iteration count to 10 (you can optionally keep request-based limits but cap at 10)
    $total_iterations = 10;
    // Alternative: Respect request limit but prevent exceeding 10
    // $total_iterations = isset( $_REQUEST['revise_limit'] ) ? min( intval( $_REQUEST['revise_limit'] ), 10 ) : 10;

    // Fetch up to 10 changed listings (use secure prepared statement)
    $listings = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT id, account_id FROM {$wpdb->prefix}ebay_auctions 
             WHERE status = 'changed' 
             ORDER BY id ASC 
             LIMIT %d",
            $total_iterations
        )
    );

    // Loop through each listing (this is our 10 iterations)
    foreach ( $listings as $listing ) {
        // --------------------------
        // Insert your actual revision logic here
        // Example: Call your existing revise function
        // revise_single_ebay_listing( $listing->id, $listing->account_id );
        // --------------------------

        // Mark listing as processed to avoid re-running on next execution
        $wpdb->update(
            "{$wpdb->prefix}ebay_auctions",
            array( 'status' => 'revised' ), // Update to your preferred post-processing status
            array( 'id' => $listing->id ),
            array( '%s' ),
            array( '%d' )
        );
    }
}

Key Improvements & Explanations:

  • Fixed Iteration Control: We hardcode the iteration count to 10 (with an optional fallback to respect request limits while capping at 10) to guarantee consistent execution.
  • Secure Limited Query: Added LIMIT %d with $wpdb->prepare to safely fetch exactly 10 listings, avoiding SQL injection risks and unnecessary database load.
  • Iteration Loop: The foreach loop runs once per listing, delivering the 10 iterations you need.
  • Prevent Reprocessing: Included an update query to change the listing's status after processing, so it won't be picked up in future cron runs.

Quick Tips:

  • Replace the placeholder revision logic with your actual code to handle the eBay listing updates.
  • Adjust the ORDER BY clause (e.g., modified_date DESC) to prioritize which listings get processed first.
  • Keep $wpdb->prepare in place whenever using dynamic values in queries—it's a critical WordPress security best practice.

内容的提问来源于stack exchange,提问作者Vito Santimone

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:50:39