1

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.

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?

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);

        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.