Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In the first function below, inside the foreach, I'm calling out to the 2nd function in order to test for the occurence of matching category ids that I want to remove from the category listing.

However, the way I'm doing it in the 2nd function seems to me to be pretty crude, for lack of a better term. How could I improve this lookup?

function admin_trim_category_description( $terms, $taxonomies )
{
    if( 'category' != $taxonomies[0] )return $terms;

    foreach( $terms as $key=>$term ) 
        { 
        $terms[$key]->description = strip_tags(substr( $term->description, 0, 75 ))."..."; 
        if(ce4_get_utility_cats($terms[$key]->term_id))
            {
            unset($terms[$key]);
            }
        } 
        return $terms;
}

function  ce4_get_utility_cats($cat_id)
{
    if($cat_id == get_cat_ID('category1') OR $cat_id == get_cat_ID('hidden') OR $cat_id == get_cat_ID('category2') OR $cat_id == get_cat_ID('category3'))
        {
        return true;
        }
        else
        {
        return false;
        }
}

EDIT: The above functions are called in the following manner...

add_action( 'admin_head-edit-tags.php', 'admin_edit_tags' );
function admin_edit_tags(){
    add_filter( 'get_terms', 'admin_trim_category_description', 10, 2 );
    }
share|improve this question
    
Where are you calling admin_trim_category_description() from? What's your context and use-case? Also, what does $terms contain? –  MikeSchinkel Jan 25 '11 at 5:20
    
And this is a nice listing of php array functions: w3schools.com/php/php_ref_array.asp –  hakre Jan 25 '11 at 5:23
    
@Mike: Good point, I've updated my question to provide the context –  Scott B Jan 25 '11 at 15:38

3 Answers 3

up vote 4 down vote accepted
function admin_trim_category_description( $terms, $taxonomies ){
  if( 'category' != $taxonomies[0] )return $terms;
  $whatever_categories = array('category1', 'hidden', 'category2', 'category3');

  foreach( $terms as $key => $term)
    if(!in_array($terms[$key]->name, $whatever_categories)) $terms[$key]->description = strip_tags(substr( $term->description, 0, 75 ))."...";
    else unset($terms[$key]);


  return $terms;
}
share|improve this answer
    
+1. avoids all those pesky db queries. –  Cronco Jan 25 '11 at 5:24
    
+1 Works great. I knew there was a better way :) in_array will come in handy in the future. –  Scott B Jan 25 '11 at 15:44

The only thing I can see which is blatantly inefficient is the fact that you call get_cat_ID for every category for every term. That's a db query for each function call.

It would be more efficient to call those functions before the foreach and put them in an array sent as a parameter to the function.

share|improve this answer

Perhaps try adding the terms you're checking for into an array, and then just check if the $cat_id is in the array. I want to say that there are functions similar to that, but I can't place them offhand. It would eliminate the need for the OR's

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.