0

I have a custom post type, 'fee', which has a custom taxonomy, 'fee-category'.

I'm trying to look up IDs for items in the taxonomy by name. Should I not be able to get it using get_term_by()?

Something like this: get_term_by('name', 'Sample Name', 'fee-category')

I even tried just looking for taxonomies by ID, using IDs I know exist and no luck.

Anyone know which function I should be using?

Perhaps there's something in my code registering the taxonomy? Here it is:

  add_action( 'init', 'create_fee_taxonomy' );

  function create_fee_taxonomy() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
            'name' => _x( 'Fee Category', 'taxonomy general name' ),
            'singular_name' => _x( 'Fee Category', 'taxonomy singular name' ),
            'search_items' => __( 'Search Fee Categories' ),
            'all_items' => __( 'All Fee Categories' ),
            'parent_item' => __( 'Parent Fee Category' ),
            'parent_item_colon' => __( 'Parent Fee Category:' ),
            'edit_item' => __( 'Edit Fee Category' ),
            'update_item' => __( 'Update Fee Category' ),
            'add_new_item' => __( 'Add New Fee Category' ),
            'new_item_name' => __( 'New Fee Category Name' ),
            'menu_name' => __( 'Fee Category' ),
    );

    register_taxonomy( 'fee-category', array( 'fee' ), array(
            'public' => true,
            'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'show_admin_column' => true,
            'query_var' => true,
    ) );
}
  • 1
    The function get_term_by returns an object, you could get the ID from the object. $obj = get_term_by('name', 'Sample Name', 'fee-category'); echo $obj->term_id; – Shazzad Jan 20 '14 at 9:21
  • That's what I thought, but this var_dump( get_term_by('name', 'Sample Name', 'fee-category') ); is returning this: bool(false) And 'Sample Name' does exist. I also substituted 'id', '12' (which is the id for 'Sample Name') for the first two variables and got the same result. – Sandbox Wizard Jan 20 '14 at 15:00
1

I just ran a quick test on a site that uses custom taxonomies. The sample code that you used works for me.

I did the following

$test_term_by = get_term_by('name', 'Sample Name', 'my-custom-taxonomy');

// note that this produces a "stdclass object", not an array.

echo '<pre>';
print_r($test_term_by);
echo '</pre>';

echo $test_term_by->term_taxonomy_id; // one of the values of our object.

Perhaps you want to do some testing with the get_term_link( $term, $taxonomy ) function, which is a bit more straightforward in it's output, and can be useful for your debugging.

  • PS: bool(false) is the result I get for terms that don't exist or were deleted. – Manu Jan 20 '14 at 21:21

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.