修改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 %dwith$wpdb->prepareto safely fetch exactly 10 listings, avoiding SQL injection risks and unnecessary database load. - Iteration Loop: The
foreachloop runs once per listing, delivering the 10 iterations you need. - Prevent Reprocessing: Included an
updatequery 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 BYclause (e.g.,modified_date DESC) to prioritize which listings get processed first. - Keep
$wpdb->preparein place whenever using dynamic values in queries—it's a critical WordPress security best practice.
内容的提问来源于stack exchange,提问作者Vito Santimone
相关产品推荐
相关产品推荐

