<?php

/*
Plugin Name: Show empty category archive
Description: Show category archive pages for existing categories containing no posts instead of sending 404 Not Found.
Author: Sam
Version: 0.1
*/

// There is no action after get_posts & before handle_404() :(
add_filter('found_posts', 'fake_found_category');
add_action('wp', 'did_permalink');

function did_permalink() {
	$GLOBALS['wp']->did_permalink = true;
}

function fake_found_category($posts) {
	$remember = error_reporting();
	error_reporting(E_ALL);
	
	if	(	$GLOBALS['wp_query']->is_category
			&& empty($posts)
			&& $category = get_category_by_path(get_query_var('category_name'), false)
		)
	{
		$GLOBALS['wp']->did_permalink = false;
		if (!get_query_var('cat')) {
			set_query_var('cat', $category->cat_ID);
		}
	}
	// var_dump($posts, $GLOBALS['wp_query']->is_category, $GLOBALS['wp']->did_permalink, get_query_var('category_name'), $category, single_cat_title('', false) /*$GLOBALS['wp_query']*/);
	error_reporting($remember);
	return $posts;
}

?>