Opened 12 years ago
Closed 12 years ago
#28159 closed defect (bug) (fixed)
get_body_class makes a raw, uncacheable query for child posts
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 4.0 | Priority: | normal |
| Severity: | normal | Version: | 2.8 |
| Component: | Themes | Keywords: | has-patch commit |
| Focuses: | template | Cc: |
Description
get_body_class() calls $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'page' AND post_status = 'publish' LIMIT 1", $page_id) ) just so it can add a single class.
It also accesses the database directly this way, and is hence not cacheable at all, causing a query to be made on every page for something that's not always needed (but can't be disabled).
Attachments (2)
Change History (9)
#1
@
12 years ago
- Focuses template added
- Keywords has-patch added
- Milestone changed from Awaiting Review to 4.0
- Version set to 2.8
#2
@
12 years ago
Using get_posts() will actually make it ~ 5x slower and doesn't really do any useful caching.
#3
follow-up:
↓ 4
@
12 years ago
This would work, and would leverage post caches:
$parent_page = get_post( $page_id );
if ( 'page' === $parent_page->post_type && 'publish' === $parent_page->post_status ) {
}
#4
in reply to:
↑ 3
@
12 years ago
Replying to nacin: Sure, but that doesn't really give us the page's children. Perhaps "caching" the query result in a _wp_has_children meta and flushing it in wp_insert_post() could work?
#5
follow-up:
↓ 6
@
12 years ago
- Keywords commit added
How about get_pages()? It does use caching: tags/3.9.1/src/wp-includes/post.php#L4162.
#6
in reply to:
↑ 5
@
12 years ago
Replying to SergeyBiryukov: looks reasonable.
Is 28159.diff what we're looking for?
Before:
After:
Introduced in r11053.