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

WordPress多分类产品站点维护:分类结构优化及批量处理求助

Hey Joey, let's walk through how to fix your site's taxonomy structure by migrating those brand/retailer subcategories to proper custom taxonomies—this will make your site more scalable and easier to maintain long-term. Here's a step-by-step solution tailored to your setup:

1. Register Custom Taxonomies First

First, we need to create two dedicated custom taxonomies: brand for your 300+ brands, and retailer for your 20 retailers. This keeps them separate from your default product categories, which is the industry best practice.

Add this code to your theme's functions.php file (or better yet, a custom plugin to avoid losing changes when you update your theme):

function register_product_custom_taxonomies() {
    // Register Brand Taxonomy
    $brand_args = array(
        'labels' => array(
            'name' => 'Brands',
            'singular_name' => 'Brand',
            'search_items' => 'Search Brands',
            'all_items' => 'All Brands',
            'edit_item' => 'Edit Brand',
            'update_item' => 'Update Brand',
            'add_new_item' => 'Add New Brand',
            'new_item_name' => 'New Brand Name',
            'menu_name' => 'Brands',
        ),
        'hierarchical' => true, // Keep this if you might need sub-brands later
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'brand' ),
    );
    register_taxonomy( 'brand', 'product', $brand_args );

    // Register Retailer Taxonomy
    $retailer_args = array(
        'labels' => array(
            'name' => 'Retailers',
            'singular_name' => 'Retailer',
            'search_items' => 'Search Retailers',
            'all_items' => 'All Retailers',
            'edit_item' => 'Edit Retailer',
            'update_item' => 'Update Retailer',
            'add_new_item' => 'Add New Retailer',
            'new_item_name' => 'New Retailer Name',
            'menu_name' => 'Retailers',
        ),
        'hierarchical' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'retailer' ),
    );
    register_taxonomy( 'retailer', 'product', $retailer_args );
}
add_action( 'init', 'register_product_custom_taxonomies' );

After adding this, you'll see new "Brands" and "Retailers" menus in your WordPress admin dashboard.

2. Bulk Migrate Existing Categories to Custom Taxonomies

Manually migrating 300+ brands is unrealistic, so we'll use a PHP script to automate this. First, back up your database—this is non-negotiable in case something goes wrong during the migration.

You can run this script via a custom plugin, or temporarily add it to functions.php (make sure to remove it after migration):

function migrate_brand_retailer_categories() {
    // Only run this once—remove or comment out after migration!
    if ( ! current_user_can( 'manage_options' ) ) return;

    // Migrate Brands (parent category ID 187)
    $brand_parent_id = 187;
    $brand_categories = get_terms( array(
        'taxonomy' => 'category',
        'parent' => $brand_parent_id,
        'hide_empty' => false,
    ) );

    foreach ( $brand_categories as $cat ) {
        // Create the brand term if it doesn't already exist
        $brand_term = term_exists( $cat->name, 'brand' );
        if ( ! $brand_term ) {
            $brand_term = wp_insert_term( $cat->name, 'brand', array(
                'slug' => $cat->slug,
                'description' => $cat->description,
            ) );
        }

        // Fetch all products assigned to this old category
        $products = get_posts( array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'term_id',
                    'terms' => $cat->term_id,
                ),
            ),
        ) );

        // Assign each product to the new brand term
        foreach ( $products as $product ) {
            wp_set_object_terms( $product->ID, $brand_term['term_id'], 'brand', true );
        }
    }

    // Migrate Retailers (parent category ID 186)
    $retailer_parent_id = 186;
    $retailer_categories = get_terms( array(
        'taxonomy' => 'category',
        'parent' => $retailer_parent_id,
        'hide_empty' => false,
    ) );

    foreach ( $retailer_categories as $cat ) {
        // Create the retailer term if it doesn't already exist
        $retailer_term = term_exists( $cat->name, 'retailer' );
        if ( ! $retailer_term ) {
            $retailer_term = wp_insert_term( $cat->name, 'retailer', array(
                'slug' => $cat->slug,
                'description' => $cat->description,
            ) );
        }

        // Fetch all products assigned to this old category
        $products = get_posts( array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'term_id',
                    'terms' => $cat->term_id,
                ),
            ),
        ) );

        // Assign each product to the new retailer term
        foreach ( $products as $product ) {
            wp_set_object_terms( $product->ID, $retailer_term['term_id'], 'retailer', true );
        }
    }

    // Show success notice once migration is done
    add_action( 'admin_notices', function() {
        echo '<div class="notice notice-success is-dismissible"><p>Brand and Retailer migration completed successfully!</p></div>';
    } );
}
// Run once—comment this line out after execution to avoid re-running
add_action( 'admin_init', 'migrate_brand_retailer_categories' );

How to use this:

  1. Add the code to your site.
  2. Log in to your admin and load any page—this triggers the migration.
  3. Once you see the success notice, immediately comment out or remove the add_action( 'admin_init', ... ) line to prevent re-running the script.
3. Update Frontend Templates & Queries

Now you need to update your site's frontend to use the new custom taxonomies instead of the old categories:

  • Archive Pages: Create template files named taxonomy-brand.php and taxonomy-retailer.php (copy your existing category.php and modify it to pull terms from the new taxonomies).
  • Product Pages: Update your single product template to display the brand/retailer from the brand/retailer taxonomies instead of the old category terms. Use code like:
    // Display Brand on Product Page
    $brands = wp_get_object_terms( get_the_ID(), 'brand' );
    if ( ! empty( $brands ) ) {
        echo '<div class="product-brand">Brand: <a href="' . get_term_link( $brands[0] ) . '">' . $brands[0]->name . '</a></div>';
    }
    
    // Display Retailer on Product Page
    $retailers = wp_get_object_terms( get_the_ID(), 'retailer' );
    if ( ! empty( $retailers ) ) {
        echo '<div class="product-retailer">Retailer: <a href="' . get_term_link( $retailers[0] ) . '">' . $retailers[0]->name . '</a></div>';
    }
    
  • Menus & Navigation: Update any menu items that linked to the old brand/retailer category pages to point to the new custom taxonomy archive URLs.
4. Verify & Clean Up
  • Validate Data: Check a handful of products to ensure they have the correct brand/retailer assigned in the new taxonomies.
  • Clean Up Old Categories: Once you confirm the migration is successful, you can delete the old parent categories (186 and 187) and their subcategories. Again, double-check your backup before deleting anything.
  • Flush Permalinks: Go to Settings → Permalinks and click "Save Changes" to refresh your site's URL structure for the new taxonomies.
Pro Tips
  • If you're comfortable with WP-CLI, you can use commands like wp term list category --parent=187 to export your existing brand list, or wp term create brand "Brand Name" to create terms faster.
  • If your site uses an SEO plugin (like Yoast SEO), don't forget to configure the SEO settings for your new brand and retailer taxonomies.
  • Clear all site cache (plugin cache, server cache, CDN cache) after migration to ensure visitors see the updated content.

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

火山引擎 最新活动