Well, it seems to be a problem with category_cache, because I wrote a new function instead:
function in_categorie($category) { // Check if the current post is in the given category
global $post, $tablecategories, $tablepost2cat, $wpdb;
$cats = '';
$categories = $wpdb->get_results("
SELECT category_id, cat_name, category_nicename, category_description, category_parent
FROM $tablecategories, $tablepost2cat
WHERE $tablepost2cat.category_id = cat_ID AND $tablepost2cat.post_id = $post->ID
");
foreach ($categories as $cat) :
$cats[] = $cat->category_id;
endforeach;
if ( in_array($category, $cats) )
return true;
else
return false;
}
and it works with that new function!
That being said, I am not really satisfied with this approach, because it effectively just filters out the posts IN THE CURRENT REQUEST that do not match the conditions. The problem with this is that there is no way to tell in advance if the page is going to contain ANY posts and then, if some posts do match the conditions, on a page-based system, that means some pages can have 3 posts while other pages might have 5 posts, etc.
What I would really like is code that actually ONLY requests from the database those posts that do match the conditions, so that I can tell right away if there are no posts that match the conditions, and also so that I can control how many posts per page I am going to get.
Is this possible without becoming a WordPress expert? :-/