Delete taxonomy terms of a specific level or superior

I had produced a code that automatically creates taxonomy terms when visitors visit my web site : i found out that thousands of terms had been created , cloaking my database. only solution was to use the following code to automatically delete terms of level >1

 

set_time_limit(0);
 error_reporting(E_ERROR | E_WARNING | E_PARSE);

  function myErrorHandler($errno, $errstr, $errfile, $errline) {
    echo "<br>error" . $errstr . $errfile;
    return true;
  }
if (!strstr($_SERVER["SERVER_NAME"],"cadiou")) exit();
  //$old_error_handler = set_error_handler("myErrorHandler");
require( './wp-load.php' ); 

$tax_name = 'gamme';
$terms = get_terms( $tax_name );
function sort_terms_hierarchicaly(Array &$cats, Array &$into, $parentId = 0, $level=0)
{
    foreach ($cats as $i => $cat) {
        if ($cat->parent == $parentId) {
            $into[$cat->term_id] = $cat;
			echo"<br> $level ".str_repeat(" - " ,$level) . $cat->term_id . " : " .  $cat->slug;
			if ($level>1) wp_delete_term($cat->term_id, "gamme");
            unset($cats[$i]);
        }
    }

    foreach ($into as $topCat) {
        $topCat->children = array();
        sort_terms_hierarchicaly($cats, $topCat->children, $topCat->term_id, $level++);
    }
}$categories = get_terms('gamme', array('hide_empty' => false));
$categoryHierarchy = array();
sort_terms_hierarchicaly($categories, $categoryHierarchy);

 

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *