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?

1 Answer
1

try this..

$filter_terms = array();
foreach ($widget['select'] as $key => $name) {
    $filter_terms[$key] = get_term_by( 'name', $name, 'portfolio-categories');
}

Leave a Reply

Your email address will not be published. Required fields are marked *