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.
You have chosen to tackle a hard problem for one so new to PHP. 🙂 This might get you closer to your goal: