You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用PHP在WordPress中上传自定义文章的时长增加问题

Got it, let's break down why your batch WordPress custom post uploads are taking way longer now, and walk through actionable fixes. I’ve tackled similar bulk import slowdowns before, so here’s what to check:

Possible Causes for the Slowdown

  1. Redundant File System Scans & Unchecked Duplicates
    If your code doesn’t cache the folder structure or skip folders that already have corresponding posts, it’s re-scanning the entire C:/******/S1/ directory every time—and re-uploading content that’s already in WordPress. As the number of folders grows, this repeated IO work adds up fast.

  2. Synchronous Attachment Processing Overhead
    WordPress generates multiple thumbnails for every image by default (think thumbnail, medium, large, etc.). For 10-15 images per post × 100 posts, that’s 1000-1500 images, each triggering multiple file writes and database updates. Doing this synchronously for every single image creates a massive bottleneck.

  3. Groups Plugin’s Per-Post Access Control Overhead
    If you’re setting Groups permissions immediately after creating each post, you’re making repeated database calls for every single post. Groups likely writes to its own access control tables each time, and if it’s hooking into post-save actions, those extra queries pile up as your dataset grows.

  4. Database Bloat & Missing Indexes
    As you add more posts, attachments, and Groups access rules, your WordPress database tables (especially wp_posts, wp_postmeta, and Groups-specific tables like wp_groups_post_access) can get bloated. Without proper indexes, queries to check existing posts or link attachments take longer to execute.

  5. Unoptimized Hook Triggers
    Other plugins or WordPress core hooks might be firing on every post/attachment save—like image compression plugins, SEO meta generators, or analytics trackers. Each of these adds extra processing time per item, which becomes noticeable at scale.

Optimization Solutions

Let’s fix these one by one, starting with the easiest wins:

1. Cut Redundant Work with Caching & Duplicate Checks

  • Pre-scan & Cache Folder Structure: Use PHP’s RecursiveDirectoryIterator + RecursiveIteratorIterator to scan C:/******/S1/ once at the start, storing all folder paths and names in an array. This avoids repeated IO calls during the upload loop.
  • Skip Existing Posts: Before processing a folder, query WordPress to check if a post with the folder’s name (as title or a custom meta field) already exists. Use a single WP_Query with post_type set to your custom post type and post_title matching the folder name—skip any folders that have a matching post.

2. Speed Up Attachment Processing

  • Asynchronous Thumbnail Generation: Instead of generating thumbnails during upload, use WP Cron to queue thumbnail generation for after the bulk import. You can disable synchronous generation temporarily with add_filter('wp_generate_attachment_metadata', '__return_false'); during the import, then trigger a cron job to generate all missing thumbnails once everything is uploaded.
  • Trim Unnecessary Image Sizes: Go to Settings > Media and remove any unused image sizes, or use remove_image_size() in your code to eliminate sizes you don’t need. Fewer thumbnails = less processing time.
  • Batch Database Writes: Collect all attachment data first, then use $wpdb->insert() in bulk (or build a single INSERT statement with multiple rows) to add attachments to the database, instead of inserting one at a time. Then batch-update the post_parent field to link attachments to their posts.

3. Optimize Groups Access Control

  • Batch Permission Updates: Don’t set permissions for each post as you create it. Collect all new post IDs in an array, then use Groups’ API (or direct $wpdb queries to the Groups access table) to assign permissions in one go. Check Groups’ documentation for bulk methods—if none exist, you can write a single INSERT query to link all post IDs to the target group ID.
  • Delay Permission Setting: Run the Groups permission assignment as a final step after all posts and attachments are uploaded. This avoids interleaving permission checks/writes with the already-heavy upload process.

4. Tune Your Database

  • Add Missing Indexes: Check your database tables:
    • For wp_posts: Add an index on (post_type, post_title) to speed up existing post checks.
    • For wp_postmeta: Add a composite index on (post_id, meta_key) to speed up meta queries.
    • For Groups’ wp_groups_post_access: Add an index on (post_id, group_id) to speed up permission assignments.
  • Enable Object Caching: Use a caching layer like Redis or Memcached with WordPress to reduce repeated database queries. Plugins like Redis Object Cache make this easy to set up.

5. Reduce Hook Overhead

  • Temporarily Disable Non-Essential Plugins: During bulk upload, disable plugins like image compressors, SEO tools, or analytics that trigger on post/attachment save. You can re-enable them after the import finishes.
  • Unhook Unnecessary Actions: Use remove_action() in your code to disable specific hooks that aren’t needed during the import (e.g., if a plugin hooks into save_post to generate meta data, disable it temporarily).

Bonus: Script Tuning

  • Increase PHP Limits: At the top of your script, add:
    ini_set('memory_limit', '256M');
    set_time_limit(0);
    
    This prevents memory exhaustion and timeouts during long runs.
  • Parallel Processing (Advanced): If you’re on a Linux server, use PHP’s pcntl_fork to split the folder list into chunks and process them in parallel. For Windows, you could use a task runner or split the folder list into smaller batches manually.

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

火山引擎 最新活动