3

My code below. It does not work to delete using the terms. I need it to work like this and not delete by ID.

$terms = get_terms( 'MY_TAXONOMY', array( 
                        'orderby' => 'name',
                        'order'   => 'ASC',
                        'exclude'  => array(),
) );
$exclude = array("MY TERM", "MY TERM 2", "MY TERM 3");
$new_the_category = '';
foreach ( $terms as $term ) {
    if (!in_array($term->term_name, $exclude)) {
        $new_the_category .= '<div class="post hvr-grow"><li><strong><a id="lista" href="'.esc_url( get_term_link( $term ) ) .'">'.$term->name.'</a>'. ' ('. $term->count . ')</strong></li></div>';
    }
}
echo substr($new_the_category, 0);
ilim
  • 4,477
  • 7
  • 27
  • 46
J.Z
  • 57
  • 1
  • 1
  • 10

3 Answers3

4

Your code is working fine just need to replace $term->term_name to $term->name then it should work fine. see below code for reference.

$terms = get_terms( 'MY_TAXONOMY', array( 
                        'orderby' => 'name',
                        'order'   => 'ASC',
                        'exclude'  => array(),
) );
$exclude = array("MY TERM", "MY TERM 2", "MY TERM 3");
$new_the_category = '';
foreach ( $terms as $term ) {
if (!in_array($term->name, $exclude)) {
$new_the_category .= '<div class="post hvr-grow"><li><strong><a id="lista" href="'.esc_url( get_term_link( $term ) ) .'">'.$term->name.'</a>'. ' ('. $term->count . ')</strong></li></div>';
}
}
echo substr($new_the_category, 0);
Mukesh Panchal
  • 1,956
  • 2
  • 18
  • 31
2

You can get the term_ids of the ones you want excluded by using get_term_by() on the terms you want omitted. Then you can pass those ids as the exclude argument.

As a note, the second $args array in get_terms() has been deprecated, so you should move MY_TAXONOMY into the arguments with the key taxonomy.

Also I'm not sure why you're echoing a substring that starts at 0 without an end point so I removed that. I also removed the variable concatenation and just echoed the string in the foreach loop.

$exclude_ids   = array();
$exclude_names = array("MY TERM", "MY TERM 2", "MY TERM 3"); // Term NAMES to exclude

foreach( $exclude_names as $name ){
    $excluded_term = get_term_by( 'name', $name, 'MY_TAXONOMY' );
    $exclude_ids[] = (int) $excluded_term->term_id; // Get term_id (as a string), typcast to an INT
} 

$term_args = array(
    'taxonomy' => 'MY_TAXONOMY',
    'orderby' => 'name',
    'order'   => 'ASC',
    'exclude' => $exclude_ids
);

if( $terms = get_terms( $term_args ) ){
    // If we have terms, echo each one with our markup.
    foreach( $terms as $term ){
        echo '<div class="post hvr-grow"><li><strong><a id="lista" href="'.esc_url( get_term_link( $term ) ) .'">'.$term->name.'</a>'. ' ('. $term->count . ')</strong></li></div>';
    }
}
Xhynk
  • 13,513
  • 8
  • 32
  • 69
1

Simply like this:

$terms = get_terms(array(
  'hide_empty' => false, // Also use True. 
  'taxonomy' => 'TAXONOMY',
  'order'   => 'DESC', // Optional
  'exclude' => array('416','[term_id]','[term_id]') // Or you can use Include
  )
);

foreach ( $terms as $term ) {
  $term_name = $term->name;
}

I didn't write your full code but I hope you can get what you want. Moto should be writing less code but efficiently.