#46 closed defect (bug) (fixed)
in_category() causes errors for posts not stored in the cache
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | minor | Version: | |
Component: | General | Keywords: | |
Focuses: | Cc: |
Description
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;
endforeach;
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)
#7
@
17 years ago
- Resolution changed from 10 to 20
- Status changed from assigned to closed
Call update_post_caches() from within query_posts() and get_posts().
http://wordpress.org/pipermail/cvs_wordpress.org/2004-June/000114.html
http://wordpress.org/pipermail/cvs_wordpress.org/2004-June/000113.html
Note that I classified this as "minor", even though it causes php errors, because I get the feeling that in_category() is not one of those functions that get used by most people.