简体   繁体   中英

Wordpress get_terms including translations

Im trying to delete all custom taxonomy objects including translations made with WPML.

$terms = get_terms('product-category');
foreach ($terms as $term) {
    wp_delete_term($term->term_id, 'product-category');
}

What this does, is delete all main language taxonomies but all translations are left. What is the right way to delete all taxonomies with their translations. It's also important that translation links in *_icl_translations table would get removed for the taxonomies.

You need to use icl_object_id function. ie:

icl_object_id( {term_id}, {taxonomy}, false, {language} );

Here is full example for easy to understand:

$all_languages = icl_get_languages();
$terms = get_terms('product-category');
foreach ($terms as $term) {
    wp_delete_term($term->term_id, 'product-category');
    foreach ($all_languages as $lang => $row) {
        if ($term_id = icl_object_id( $term->term_id, 'product-category', false, $lang )){
            wp_delete_term($term_id, 'product-category');
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM