WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#12989 closed defect (bug) (fixed)

Object term cache not updated for custom post types

Reported by: greenshady Owned by: ryan
Milestone: 3.0 Priority: normal
Severity: normal Version: 3.0
Component: Cache API Keywords: has-patch dev-reviewed
Focuses: Cc:

Description

I was tinkering around with custom post types with their own custom taxonomies today and realized I had a page with 70+ database queries and only 10 "posts" listed, each with 5 term lists. This was with running file-based caching, so I knew something was wrong.

The problem seemed to be coming from calls to get_the_term_list() for the custom taxonomies. Each time this was called, it was querying the database. This wouldn't be so bad if we were simply listing 10 "posts", each with one call to the get_the_term_list().

get_the_term_list() calls get_the_terms(), which checks for the object term cache like so:

$terms = get_object_term_cache( $id, $taxonomy );

Unfortunately, with custom post types, this ALWAYS returns false. So, the function must get this information from the database.

After some more digging, I found my way to the update_post_caches() function in wp-includes/post.php, which is where I think the problem is. It explicitly says the object type is post:

update_object_term_cache($post_ids, 'post');

Making a quick change of post to ticket (my custom post type) and a page refresh fixed my database query issue.

I may be off base about where the problem originates but this seems to be it. I tried to dig as deep as I could on this one and provide the necessary steps.

Attachments (2)

12989.diff (1.3 KB) - added by ryan 8 years ago.
12989.2.diff (1.7 KB) - added by scribu 8 years ago.
Check 'any' post_type

Download all attachments as: .zip

Change History (10)

#1 @ryan
8 years ago

When WP_Query::get_posts() calls update_post_caches() it should probably pass along the post type. update_post_caches() would need to know how to handle multiple post type queries and the 'any' post type (perhaps just not try to cache those). Attached is an untested start on a patch.

@ryan
8 years ago

#2 @ryan
8 years ago

The patch needs to check for != 'any' as well. I forgot to add it.

#3 @greenshady
8 years ago

The current patch removed 50 database queries when looping through 10 posts from my above example.

#4 @scribu
8 years ago

  • Keywords has-patch added

#5 @scribu
8 years ago

Related: #12611

@scribu
8 years ago

Check 'any' post_type

#6 @scribu
8 years ago

12989.2.diff:

  • check 'any' != $post_type before calling update_object_term_cache
  • merge patch from #12611

#7 @westi
8 years ago

  • Keywords dev-reviewed added

Patch looks ok

#8 @nacin
8 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [14310]) Update object term cache for custom post types. props scribu, fixes #12989.

Note: See TracTickets for help on using tickets.