WordPress优化:隐藏作为其他标签子组的从属标签
Hey there! Let's fix that annoying redundant tag problem on your WordPress site with 10k+ posts. When you've got tags like usa where every post tagged usa also has america (but not the other way around), we can eliminate those child tags from appearing anywhere on your site. Here's how to do it step by step:
Step 1: Identify Which Tags Are Child Tags
First, we need to find all tag pairs where one tag is a complete subset of another. We can run a SQL query on your WordPress database to get this list.
SQL Query to Find Child Tags
Connect to your database (via phpMyAdmin, WP-CLI, or your hosting tool) and run this query (replace wp_ with your table prefix if it's different):
SELECT t1.term_id AS child_tag_id, t1.name AS child_tag_name, t2.term_id AS parent_tag_id, t2.name AS parent_tag_name FROM wp_terms t1 JOIN wp_term_taxonomy tt1 ON t1.term_id = tt1.term_id AND tt1.taxonomy = 'post_tag' JOIN wp_term_relationships tr1 ON tt1.term_taxonomy_id = tr1.term_taxonomy_id JOIN wp_term_relationships tr2 ON tr1.object_id = tr2.object_id JOIN wp_term_taxonomy tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id AND tt2.taxonomy = 'post_tag' JOIN wp_terms t2 ON tt2.term_id = t2.term_id WHERE t1.term_id != t2.term_id GROUP BY t1.term_id, t2.term_id HAVING COUNT(DISTINCT tr1.object_id) = ( SELECT COUNT(DISTINCT tr.object_id) FROM wp_term_relationships tr JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.term_id = t1.term_id )
This query will return all child tags (and their corresponding parent tags) where every post with the child tag also has the parent tag. Save the child_tag_id values from the results—we'll need these later.
Step 2: Hide Child Tags From Your Site
Now that we have the IDs of the redundant child tags, we can use WordPress filters to remove them from tag clouds, tag lists, and anywhere else tags are displayed.
Option 1: Filter All Tag Queries
Add this code to your theme's functions.php file (or a custom plugin) to exclude child tags from every tag retrieval:
/** * Hide redundant child tags from all tag lists */ add_filter('get_tags', 'exclude_child_tags'); function exclude_child_tags($tags) { // Replace these IDs with the child tag IDs you found in Step 1 $hidden_tag_ids = array(123, 456, 789); foreach ($tags as $key => $tag) { if (in_array($tag->term_id, $hidden_tag_ids)) { unset($tags[$key]); } } return $tags; }
Option 2: Filter Only the Tag Cloud
If you only want to hide child tags from the tag cloud (and keep them elsewhere), use this filter instead:
/** * Hide redundant child tags from the tag cloud */ add_filter('wp_tag_cloud', 'filter_child_tags_from_cloud', 10, 2); function filter_child_tags_from_cloud($return, $args) { // Replace these IDs with the child tag IDs you found in Step 1 $hidden_tag_ids = array(123, 456, 789); // Fetch tags excluding the child ones $filtered_tags = get_tags(array( 'exclude' => $hidden_tag_ids, 'orderby' => $args['orderby'], 'order' => $args['order'] )); // Rebuild the tag cloud with filtered tags $args['tags'] = $filtered_tags; return wp_tag_cloud($args); }
Step 3: Optional - Automate Maintenance (For Future Tags)
If you keep adding new tags, you'll want to periodically re-run the SQL query to find new child tags. For a more automated approach, you could:
- Create a WP-CLI command that runs the query and updates the
$hidden_tag_idsarray automatically. - Add an admin page where you can trigger the tag check and update the hidden list with one click.
Important Notes
- Backup your database before running any SQL queries or modifying code—better safe than sorry!
- Test these changes on a staging site first to avoid breaking your live site.
- If you have a massive number of tags, the SQL query might take a minute to run. Be patient, or add indexes to your
wp_term_relationshipstable (onobject_idandterm_taxonomy_id) to speed it up.
内容的提问来源于stack exchange,提问作者nyan

