I want to get category id by name so I am using get_term_by

This is working fine when name is simple but when '&' is coming in term name it is not fetching category id.

e.g I have a term named "Website Development & Designing" under skills taxonomy and I am using following query to get its term id.

$value = "Website Development & Designing"

but it does not return term object :(

2 Answers 2


The characters <, >, &, " and ' (less than, greater than, ampersand, double quote and single quote) are enconded in term names. & becomes &amp;. This is done by passing the term object to sanitize_term() function which applies several filters. By default WordPress applies theses filters: sanitize_text_field(), wp_filter_kses() and _wp_specialchars() (see wp-includes/default-filters.php), this last function encondes <, >, &, " and '.

So, you need to do:

$value = "Website Development &amp; Designing" get_term_by('name',$value,'skills');

Or, for unknown values:

get_term_by( 'name', esc_attr( $value ), 'skills');

NOTE: use esc_attr(), not _wp_specialchars() directly because _wp_specialchars() is marked as private function and it is not intended for use by plugin or theme developers.


When you save special character in term, it is encoded as HTML entity. So the term name in database will look like Website Development &amp; Designing and you are querying Website Development & Designing

You can use esc_attr() to encode the special characters before passing it as a argument.

$value = esc_attr( 'Website Development & Designing' );
get_term_by('name', $value, 'skills');

Better I will suggest you to query terms by slug always. The name can be changed anytime and you need to update the code every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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