in_category() causes errors for posts not stored in the cache
|Reported by:||anonymousbugger||Owned by:||ryan|
Just like the summary says. When a list of posts is generated that contains more posts then the main WordPress posts list (for example, a list generated using get_posts( 'numposts=10') if your main loop only displays 5 (or 7, or 9, or 1, or any number fewer then 10) posts, running in_category() on any post past the ones pulled by the main loop causes an error.
in_category() only tries to pull the category of the post it is being run on from the category_cache; posts that are not pulled by the main WP loop are obviously not stored in the cache.
One possible solution is to redefine in_category thusly:
function in_category( $categoryID )
$postCategories = get_the_category();
$postCategoryIDs = array();
foreach ( $postCategories as $postValues ):
$postCategoryIDs = $postValues->category_id;
return in_array( $categoryID, $postCategoryIDs );
Obviously that's not the only way to do it, just something I came up with based on the existing code.
Change History (9)
- Owner changed from anonymous to rboren
- Status changed from new to assigned
- Resolution changed from 10 to 20
- Status changed from assigned to closed