Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can exclude term from get_terms by term name not by term id

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);
like image 830
J.Z Avatar asked Oct 15 '25 22:10

J.Z


1 Answers

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);
like image 58
Mukesh Panchal Avatar answered Oct 18 '25 15:10

Mukesh Panchal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!