Make WordPress Core

Opened 17 years ago

Closed 17 years ago

Last modified 9 years ago

#46 closed defect (bug) (fixed)

in_category() causes errors for posts not stored in the cache

Reported by: anonymousbugger Owned by: ryan
Milestone: Priority: normal
Severity: minor Version:
Component: General Keywords:
Focuses: Cc:


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)

#2 @anonymousbugger
17 years ago

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.

#3 @matt
17 years ago

If the post isn't in the cache it should do a query to get it.

#4 @iioshius
17 years ago

Not in 1.2. Has this been fixed in cvs?

#5 @ryan
17 years ago

How about adding a call to update_post_caches() at the end of get_posts()?

#6 @ryan
17 years ago

  • Owner changed from anonymous to rboren
  • Status changed from new to assigned

#7 @ryan
17 years ago

  • Resolution changed from 10 to 20
  • Status changed from assigned to closed

#9 @downloadbook
9 years ago


Last edited 9 years ago by SergeyBiryukov (previous) (diff)
Note: See TracTickets for help on using tickets.