Last active
February 25, 2021 15:16
-
-
Save JRyven/ac76b15f53e6421022e0e7431ef7043f to your computer and use it in GitHub Desktop.
Generate 301 Redirects for and remove Taxonomy Terms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function redirect_builder(){ | |
// CLEAN UP TAGS | |
// GATHER TAGS | |
$product_tag = 'product_tag'; | |
$tags = get_terms( array( | |
'taxonomy' => $product_tag, | |
'hide_empty' => false, | |
) ); | |
// RORDER TAGS BY COUNT THEN NAME | |
$count = array_column( $tags, 'count' ); | |
$name = array_column( $tags, 'name' ); | |
array_multisort($count, SORT_ASC, $name, SORT_ASC, $tags); | |
// FOREACH | |
foreach($tags as $tag): | |
// If the term has 1 or fewer items | |
if( $tag->count <= 1 ): | |
/* output redirect rule */ | |
// echo 'Redirect 301 /YOURPATH/'.$tag->slug.' / <br/>'; | |
/* remove term */ | |
// wp_delete_term( $tag->term_id, $product_tag ); | |
endif; | |
endforeach; | |
// CLEAN UP CATS | |
// GATHER TAGS | |
$product_cat = 'product_cat'; | |
$cats = get_terms( array( | |
'taxonomy' => $product_cat, | |
'hide_empty' => false, | |
) ); | |
// RORDER TAGS BY COUNT THEN NAME | |
$count = array_column( $cats, 'count' ); | |
$name = array_column( $cats, 'name' ); | |
array_multisort($count, SORT_ASC, $name, SORT_ASC, $cats); | |
// FOREACH | |
foreach($cats as $cat): | |
// If the term has 1 or fewer items | |
if( $cat->count <= 1 ): | |
/* output redirect rule */ | |
// echo 'Redirect 301 /YOURPATH/'.$cat->slug.' / <br/>'; | |
/* remove term */ | |
// wp_delete_term( $cat->term_id, $product_cat ); | |
endif; | |
endforeach; | |
} | |
// add_action('wp_header', 'redirect_builder'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment