I am trying to retreive a term id based on the term name using the "get_term_by" function that is build into Wordpress. However, this function only retrieves one item from the array that i get them from. But there are multiple items in this array.

This is my current code:

$filter_terms = get_term_by( 'name', $widget['select'], 'portfolio-categories');

This is what is inside $widget['select'] when I dump it: array(2) { [0]=> string(5) "Beard" [1]=> string(3) "Tag" }

So in this array "Beard" and "Tag" are two terms where I want to get the ID from, however, if I dump $filter_terms I get this:

object(WP_Term)#634 (10) { 
    ["term_id"]=> int(14) 
    ["name"]=> string(5) "Beard" 
    ["slug"]=> string(5) "beard" 
    ["term_group"]=> int(0) 
    ["term_taxonomy_id"]=> int(14) 
    ["taxonomy"]=> string(20) "portfolio-categories" 
    ["description"]=> string(0) "" 
    ["parent"]=> int(0) 
    ["count"]=> int(2) 
    ["filter"]=> string(3) "raw" 

So how do i get the above information from both of the terms, except from only one?


try this..

$filter_terms = array();
foreach ($widget['select'] as $key => $name) {
    $filter_terms[$key] = get_term_by( 'name', $name, 'portfolio-categories');
  • You are a hero sir, this indeed works. Thanks alot! – JessengBijleng Dec 22 '15 at 2:48

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.