wp query – get term id from term name

Question

Try to use get_term_by(), where first argument is a field, in your case – name. Second is your value. I used esc_attr() to make it secure. Third one is taxonomy, as example I put here category.

if ($_SERVER["REQUEST_METHOD"] == "GET" && $_GET['fname2'] && $_GET['fname']) {

    $nameb = get_term_by('name', esc_attr($_GET['fname2']), 'category');
    $namea = get_term_by('name', esc_attr($_GET['fname']), 'category');

}

//check if there is an objects 
if( $nameb instanceof WP_Term && $nameb instanceof WP_Term ){
     echo $nameb->term_id;
     echo $namea->name;
}

Reference with some examples – here

Returned object:

(object) array(
   'term_id' => 49,
   'name' => 'Term Name',
   'slug' => 'term-name',
   'term_group' => 0,
   'term_taxonomy_id' => 49,
   'taxonomy' => 'category',
   'description' => '',
   'parent' => 0,
   'count' => 286,
   'filter' => 'raw',
)

0
Gidromasservis QSC 2 months 2021-05-01T11:36:01-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse