• Hello All!
    I cannot making it work.

    I have the following custom taxonomy structure (category like) :
    A
    \–My Sub Term Parent A
    \–My Sub Term Parent Abis

    Now I want to get the FIRST sub-term object so I query like that:

    $query_term = array(
                'parent'        => 804, // id of term "A"
                'search'        => esc_attr("My Sub Term Parent A")
    );
    $cat_term_obj = get_terms($my_tax, $query_term);

    The problem is it doesn’t allow me to retrieve the exact match of the queried term name. It retrieves nothing (null). I tried with name__like parameter but it’s the same.

    Is there a way to retrieve a term in a custom taxonomy name, b ased on the exact term name ?

    Thank you for your help,
    Looic.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Your code should work for you. I’m assuming you defined $my_tax correctly somewhere, and you have no typographic errors. Just to make sure, I used your code on my system, changing the parent ID and search value to work with my taxonomy. It works fine, returning the expected term object.

    The only other thing that could go wrong is you have a plugin or theme hooked into get_terms() somehow and is corrupting your query or the term cache. Examining the actual SQL query string might reveal how things have gone bad. Hook ‘terms_clauses’ and examine the passed query clauses array, in particular ‘where’. If clauses out of context are confusing, extract the clause terms and plug them into this query:
    SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits

    Plugging that complete query into myPhpAdmin should certainly return your term. If not, determine why. If the query works, then something is certainly corrupting your query.

    Thread Starter Looic

    (@looic)

    Thank you for your answer. My bad : I omit the “hide_empty” parameter.

    However, the issue is not resolved, as the search does a %LIKE% match, not an exact one.

    In the following situation, searching for ‘GG’ returns me two objects :

    A
    \-- fooGGfoo
    \-- GG
    *** New Query=Array
    (
        [parent] => 804
        [search] => GG
        [hide_empty] =>
    )
    select statement=SELECT t.*, tt.* FROM wpmutest_63_terms AS t INNER JOIN wpmutest_63_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('my_tax') AND tt.parent = '804' AND ((t.name LIKE '%GG%') OR (t.slug LIKE '%GG%')) ORDER BY t.name ASC
     -> Output get_terms()=Array
    (
        [0] => stdClass Object
            (
                [term_id] => 1644
                [name] => fooGGfoo
                [slug] => fooggfoo
                [term_group] => 0
                [term_taxonomy_id] => 2027
                [taxonomy] => my_tax
                [description] =>
                [parent] => 804
                [count] => 0
            )
    
        [1] => stdClass Object
            (
                [term_id] => 1643
                [name] => GG
                [slug] => gg
                [term_group] => 0
                [term_taxonomy_id] => 2026
                [taxonomy] => my_tax
                [description] =>
                [parent] => 804
                [count] => 0
            )
    
    )

    Is there a way to get the object by EXACT match (not %LIKE%), so that I can avoid making a if/foreach search for the right object ?

    Also I have another issue with “&” and “‘” characters in searched terms, but I will open a new thread ??

    Thank you,
    Looic.

    Moderator bcworkz

    (@bcworkz)

    If you want an exact match, there is no need for complex queries, there will be either 0 or 1 match, so use get_term() instead.

    There are options to tighten up the get_terms() query if you can’t use an exact match but need more control than LIKE '%term_name%'. You can sort of get an exact match with ‘slug’. You can use ‘exclude’ to eliminate pesky terms that LIKE returns that you do not want. Finally, the ultimate control is to hook ‘terms_clauses’ and alter the $where clause any way you like, from just dropping one of the LIKE wildcards to dropping the LIKE and inserting an equality check.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_terms : query by exact term_name, help needed’ is closed to new replies.