Support » Developing with WordPress » get_category_by_slug work find. but get_term_by is not work in my plugin

  • Hi everyone.. I am trying to make my plugin.
    ‘get_category_by_slug’ is works find but ‘get_term_by’ is not working in my plugin folder.
    here is code..
    ——————————
    funtion get_term_id_by_slug() {
    $term = get_term_by(‘slug’, ‘sometermslug’, ‘someterm’);
    $taxID = $term->term_taxonomy_id;
    return $taxID;
    }
    ——————————

Viewing 12 replies - 1 through 12 (of 12 total)
  • Just for checking: the 3rd arg of get_term_by, ‘someterm’, is a taxonomy name.
    eg.
    ‘category’
    ‘post_tag’
    ‘your_custom_taxonomy’

    @ikaring thanks your reply.
    yes, your right. It’s a typing error.
    term = get_term_by(‘slug’, ‘sometermslug’, ‘sometaxname’);
    but still occurs..

    What is the returned value and what is the desired value?

    Pls check to see var_dump( $term ); before return $taxID;.

    funtion get_term_id_by_slug() {
    $term = get_term_by(‘slug’, ‘sometermslug’, ‘someterm’);
    $taxid = $term->term_taxonomy_id;
    //var_dump($term );
    //var_dump($taxid);
    return $taxid;
    }
    result is like this…

    var_dump($term );
    bool(false) object(WP_Term)#1251 (11) { [“term_id”]=> int(10) [“name”]=> string(5) “Paper” [“slug”]=> string(5) “paper” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(10) [“taxonomy”]=> string(4) “sort” [“description”]=> string(0) “” [“parent”]=> int(0) [“count”]=> int(15) [“filter”]=> string(3) “raw” [“term_order”]=> string(1) “0” }

    //var_dump($taxid);
    NULL int(10)

    i want return term’s ID value 10

    • This reply was modified 7 months, 1 week ago by  brign.

    And what is the returned value? NULL?

    In Codex, it saids as below, though [“term_taxonomy_id”] seems to be integer.

    Warning: string vs integer confusion! Field values, including term_id are returned in string format. Before further use, typecast numeric values to actual integers, otherwise WordPress will mix up term_ids and slugs which happen to have only numeric characters!

    Try
    var_dump( intval($taxid) );
    or change $taxid to
    $taxid = $term->term_id;

    both result is … like this

    var_dump( intval($taxid) ); = int(10)
    or change $taxid to
    $taxid = $term->term_id; = int(10)

    • This reply was modified 7 months, 1 week ago by  brign.

    @ikaring
    function aaaaa{
    $term = get_term_by(‘slug’, ‘paper’, ‘sort’);
    $taxid = $term->term_taxonomy_id;
    //var_dump( $taxid );
    //echo $taxid = 10
    return $taxid = nothing
    }
    ———————
    $taxid = aaaaa();
    echo $taxid; = nothing

    Well, dont forget paren after aaaa, function aaaaa(){

    And semi colon after return $taxid

    @ikaring thanks
    u right OTL
    function aaaaa(){
    $term = get_term_by(‘slug’, ‘paper’, ‘sort’);
    $taxid = $term->term_taxonomy_id;
    //var_dump( $taxid );
    //echo $taxid ; = 10
    return $taxid ; = nothing
    }
    —————–
    $taxid = aaaaa();
    echo $taxid; = nothing

    It’s hard;;;
    Actually i want a code.
    return term’s id value for use control the ‘location value’ of param => ‘taxonymy’ used by acf register_field_group

    here is my full code..

    add_action( ‘init’, ‘get_tax_paper_id_by_slug’, 999 );
    function get_tax_paper_id_by_slug() {
    $term = get_term_by(‘slug’, ‘paper’, ‘sort’);
    $taxid = $term->term_taxonomy_id;
    //var_dump( $taxid );
    //echo $taxid;
    return $taxid ;
    }
    if(function_exists(“register_field_group”)){
    $taxid = get_tax_paper_id_by_slug();
    .
    .
    .
    .
    .
    }

    it is not normal… anyway thanks @ikaring

    Moderator bcworkz

    (@bcworkz)

    Hey brign — sorry to interrupt. In future posts, would you please use `backticks` or the code button when you post code? It’s often useful for us to copy code snippets into a test environment in order to help you. When we copy code like you posted without the code button, it’s essentially unusable because all the quotes are improper. Helping us to help you improves your chances of getting a quality reply similar to ikaring’s excellent support! TIA 🙂

    @bcworkz
    Ok I will always use that code button~!
    Thank you. let me know…

    Since correct value is stored when echo $taxid;, it should be returned as is.

    There is a space after $taxid. Isn’t that the cause?
    return $taxid ;

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘get_category_by_slug work find. but get_term_by is not work in my plugin’ is closed to new replies.