Getting Term ID from Term Name for WordPress Query

I am trying to get term ids from term name and cannot figure out why this code is not working.

$excludes = '';
    if($filtering_excludes) {

     $exclude_terms = explode(", ", $filtering_excludes);
        foreach ($exclude_terms as $exclude_term) {

            $term = get_term_by( 'name',  $exclude_term,  $filtering_tax  );
             $exclude_term = $term->term_id;         

        }

        $excludes= implode(", ", $exclude_terms);

    }

When I do a var_dump on $exclude_term is has the term id but $excludes is still returning the term name.

Read More

I have similar code for getting the category id from name and it works without a problem — this is that code.

$excludes = '';
    if($filtering_excludes) {

     $exclude_cats = explode(", ", $filtering_excludes);
        foreach ($exclude_cats as &$exclude_cat) {
             $cat_id = get_category_by_slug($exclude_cat); 
            $exclude_cat = $cat_id->term_id;

        }

        $excludes= implode(", ", $exclude_cats);
    }

Can anyone see why my $exlcudes for terms is returning the name and not id?

Related posts

Leave a Reply

1 comment

  1. Check for variables names:

      $excludes = '';
            if($filtering_excludes) {
    
             $exclude_terms = explode(", ", $filtering_excludes);
                foreach ($exclude_terms as $exclude_term) {
    
                    $term = get_term_by( 'name',  $exclude_term,  $filtering_tax  );
                     $exclude_term_array[] = $term->term_id;         
    
                }
    
                $excludes= implode(", ", $exclude_term_array);
    
            }