Make WordPress Core

Opened 11 years ago

Closed 6 years ago

#13989 closed enhancement (fixed)

Recent Comments widget optimization

Reported by: dd32 Owned by:
Milestone: 3.5 Priority: normal
Severity: normal Version: 3.0
Component: Widgets Keywords: needs-patch dev-feedback
Focuses: Cc:


Currently when permalinks are enabled, the recent comments widget will call get_post() on each parent post to retrieve its permalink.

In the event that the posts the recent comments are on are not in the current query (for example, posts on old posts, or if we're currently on a singular view), then get_post() will perform a SQL query to load the postdata.

The attached patch introduces a cache_posts($post_ids, $term_cache, $postmeta_cache); function to mass load/cache a set of posts through the usage of WP_Query

The function is smart enough not to query for posts which are already in the Object cache (ie. If the comments are on the current page, it'll skip, or if its in a memory cache)

An example of the SQL query generated by this query:

SELECT wp_posts . * 
FROM wp_posts
WHERE 1 =1
AND wp_posts.ID IN ( 95, 98, 106 ) 
AND wp_posts.post_type IN ('post', 'page', 'attachment', 'wiki', 'note', 'odd')
AND (wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft')


id 	select_type 	table 	 type 	possible_keys 			key 	key_len 	ref 	rows 	Extra 
1	SIMPLE		wp_posts range	PRIMARY,type_status_date	PRIMARY	8		NULL	3	Using where

Attachments (1)

13989.diff (2.3 KB) - added by dd32 11 years ago.

Download all attachments as: .zip

Change History (6)

11 years ago

#1 @dd32
11 years ago

Can anyone see any issues with that attack for caching these posts?

This doesn't just apply to comments, it could be used for anything which is going to call get_post() multiple times.

IMO, It slots in beside update_object_term_cache() / update_postmeta_cache() nicely.

#2 follow-up: @nacin
11 years ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from Awaiting Triage to Future Release

In menus, we used a straight get_posts call:

get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );

#3 in reply to: ↑ 2 @iseulde
8 years ago

  • Component changed from Optimization to Widgets

#4 @chriscct7
6 years ago

  • Keywords dev-feedback added

#5 @wonderboymusic
6 years ago

  • Milestone changed from Future Release to 3.5
  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.