Opened 7 months ago
Last modified 7 months ago
#63614 new enhancement
pre_get_lastpostdate filter
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | minor | Version: | |
| Component: | Query | Keywords: | has-patch has-unit-tests |
| Focuses: | Cc: |
Description
This was suggested long ago in #40789, but the original reason for that ticket was to modify a post type, so it was closed because a different solution had resolved the problem.
However, this filter would also be useful for avoiding the massive performance problems of get_lastpostdate(). Regardless of which timezone argument you pass to get_lastpostdate(), it queries the unindexed post_date_gmt column of wp_posts, which requires a full scan of all published posts of the given post type. This could be tens of thousands of rows for large sites.
It would be better if _get_last_post_time() used the post_date column instead, but I understand that would be a breaking change (although, I think a trivial one). A pre_get_lastpostdate filter with a short-circuit, like the patch offered in #40789 would at least allow power users or a plugin to provide a more performant workaround.
There is a get_lastpostdate filter, but it runs after the expensive query so it does not help.
Change History (2)
This ticket was mentioned in PR #9053 on WordPress/wordpress-develop by @pamprn.
7 months ago
#1
- Keywords has-unit-tests added
#2
@
7 months ago
Hi @gddrt welcome to trac 🚀
@pamprn thanks for the PR!
But from what I can see, the PR adds test cases for checking the existence of the above filter but the implementation for the said filter is missing...
For adding pre_get_lastpostdate filter, consider looking at src/wp-includes/post.php inside get_lastpostdate() function
It calls _get_last_post_time() internally & from my understanding, the filter could be applied before the queries are called (the OP's aim seems to be to modify the $field argument value)
References:
get_lastpostdate() https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/post.php#L7492
_get_last_post_time() https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/post.php#L7582
Add pre_get_lastpostdate filter to allow short-circuiting expensive queries.
This introduces a new filter, pre_get_lastpostdate, to allow plugins and advanced users to bypass the default database query in get_lastpostdate(), improving performance on large sites.
Trac ticket: #63614