I am designing a website which will be used as a school PA system. Users will be posting entries into categories named for specific dates that correspond to school assemblies. We have two assemblies a week, which means my site will need a lot of categories. I only want users to see the categories which correspond to recent/upcoming assemblies. Since our homepage is simply a list of categories, and all our categories are named in mm/dd/yyyy format, I wrote the following script to only display recent and not-to-distant assemblies (categories).

echo '<ul>';
 $categories = get_categories($category_id);

 foreach ($categories as $cat) {
     $category_link = get_category_link($cat);
        if( strtotime($cat->cat_name) < strtotime('-3 days')) { }
        elseif( strtotime($cat->cat_name) > strtotime('+2 months')) { }
        else { echo '<li><a href="' .$category_link. '">' . $cat->cat_name . '</a></li>'; }

 echo '</ul>';

This code runs on my site's homepage and works perfectly. The problem is, I also want to run a similar script on the category meta box in the Wordpress site admin, so our users can only post to upcoming assemblies. Originally I hoped to modify the wp_terms_checklist function to work similarly to our homepage but had no luck. Recently, I discovered MikeSchinkel's code here. I think his method shows a lot of potential since it already has the capacity to exclude certain categories from the metabox. I have attempted to modify his code for my purposes but am having trouble combining the two scripts. Here's the closest I have been so far:

add_filter('list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2);
function yoursite_list_terms_exclusions( $exclusions, $args) 
    global $pagenow;

    foreach (get_categories() as $category) 
        if( strtotime($category->cat_name) < strtotime('-3 days')) 
            $category->slug = $oldassemblies;
        elseif( strtotime($category->cat_name) > strtotime('+2 months')) 
            $category->slug = $futureassemblies; 

    if (in_array( $pagenow, array('post.php','post-new.php') ) ) 
        $exclusions = " {$exclusions} AND t.slug NOT IN ('$oldassemblies', '$futureassemblies')" ;

    return $exclusions;  

Theoretically, I feel like this code should work but the foreach statement and the get_categories() function disrupt both the front and back ends of my site. I have had success replacing the excluded category slugs with variables declared in the same function, but as soon as I try to associate these variables with my rules, the site errors. I am still learning PHP and have tried every possible variation of combination of the code that I could think of but am still having trouble. Please let me know if you have any suggestions.

Thanks, Jonathan

Note: the edits to a meta-box are part of a series of special functions I am creating for an admin theme. All of the functions are placed inside a giant if statement so they only affect users with contributor access, so my code will not be affecting site admins.

  • I searched through core files, but couldn't find the list_terms_exclusions filter. Could you point me there?
    – kaiser
    Commented Mar 30, 2011 at 3:52
  • It's mentioned in taxonomy.php, though it seems strangely defined to me. That part of the script actually comes from MikeSchinkel's code, I never would have though to use it. It does seem to successfully exclude categories though.
    – user4308
    Commented Mar 30, 2011 at 6:48
  • The list_terms_exclusions filter can be found in taxonomy.php on line 1245.
    – t31os
    Commented Apr 2, 2011 at 11:10

1 Answer 1


You have chosen to tackle a hard problem for one so new to PHP. :-) This might get you closer to your goal:

global $wpdb;
if ( in_array( $pagenow, array('post.php', 'post-new.php') ) ) {
    foreach (get_categories() as $category) {
        $cat_time = strtotime($category->cat_name);
        if ( $cat_time < strtotime('-3 days') || $cat_time > strtotime('+2 months') )
            $exclude .= $wpdb->prepare( '%s,', $category->slug );
    $exclude = substr( $exclude, 0, -1 );
    $exclusions = " ($exclusions) AND t.slug NOT IN ($exclude)" ;
  • Hi Andy, thanks so much for your response. I tried your code and sadly it didn't quite work. I think it's definitely closer and will try to revise it going forward. Thanks for getting me started!
    – user4308
    Commented Mar 30, 2011 at 6:44
  • Sorry it didn't work. I wrote it here without any testing and I didn't provide much context. I've updated it with a missing global. You should be using it inside the same sort of filter function you started with. You should try dumping the variables to see what's in them.
    – Andy
    Commented Mar 30, 2011 at 16:14
  • Hi again, sorry I didn't respond earlier - I have been focussing other parts of the site. I have played around with this code for hours and hours at a time, and I still can't get it to work right. It seems that the get_categories() function can not be called the way it is inside this function. I have tried alternatives but have had no luck so far. I might need to used a version of get_taxonomy() or get_the_term_list(). Please let me know if you have any more ideas. Thanks!
    – user4308
    Commented Apr 14, 2011 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.