#15805 closed enhancement (wontfix)
get_page_by_title() lacks caching
Reported by: | Viper007Bond | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.1 |
Component: | Performance | Keywords: | has-patch needs-testing |
Focuses: | Cc: |
Description
get_page_by_title()
always does a database query. It should use the object cache.
Attachments (3)
Change History (14)
#3
@
11 years ago
Updated the patch for 3.6, and find that it is working as expected. This is really just an update of solarissmoke's patch.
#5
@
11 years ago
Note that 15805.diff performs an unnecessary database query before checking the cache.
15805.patch does not have this issue and still applies cleanly.
#7
@
11 years ago
I think there are a few potential issues with the suggested patch.
$post_type
should be considered in the cache key. If not, looking up the page with title "This is my title" will return the same result as a post with title "This is my title" if it is pulled from cache.- How is the cache invalidated? Theoretically, the value is being cached indefinitely (although different caching engine will likely naturally purge the cache over time), which can cause some bizarre behaviors. For instance, if a post with title "This is my title" is created, subsequently deleted, then a new post with title ""This is my title" is created, the cached value would be the ID of the original post. I think we would need something along the lines of the following code to run on post trash and delete:
$key = 'post_title_' . get_the_title( $post_id ) . get_post_type( $post_id ); wp_cache_delete( $key, 'posts' );
- I think the patch needs further refreshing as
get_page
is used in the function, whereas current code usesget_post
.
#8
@
11 years ago
On further evaluation, I think that this ticket should be closed as wontfix. get_page_by_title
is not used in core at all. To add a caching layer to the function and not address invalidation seems dangerous to me; however, to add invalidation into WordPress core for a function that is never used seems unnecessary. It seems that caching and cache invalidation of this function should be the responsibility of the plugin/theme author and not WordPress core.
#9
follow-up:
↓ 10
@
11 years ago
- Milestone 3.7 deleted
- Resolution set to wontfix
- Status changed from new to closed
I agree with tollmanz. Going further, I think the whole idea of fetching something by its title is wrong. It's not unique, or particularly identifying, and the field doesn't have an index. This is better left to WP_Query itself (i.e. a search), and I could even go for deprecating this in favor of a (new?) query variable in WP_Query.
#10
in reply to:
↑ 9
@
9 years ago
Replying to nacin:
This is better left to WP_Query itself (i.e. a search), and I could even go for deprecating this in favor of a (new?) query variable in WP_Query.
Now WP_Query has a title variable, is this worth reopening/a new ticket? My inclination is to set it up as a wrapper for get_pages
.
Cache get_page_by_title queries