[wp-trac] [WordPress Trac] #35816: Add "after_get_posts" action to `WP_Query::get_posts()`
WordPress Trac
noreply at wordpress.org
Sat Feb 13 03:22:04 UTC 2016
#35816: Add "after_get_posts" action to `WP_Query::get_posts()`
---------------------------+--------------------------
Reporter: stevegrunwell | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: 4.5
Component: Query | Version: trunk
Severity: normal | Resolution:
Keywords: | Focuses: performance
---------------------------+--------------------------
Comment (by boonebgorges):
@lpawlik This is a promising technique. But it doesn't distinguish between
multiple instances of `WP_Query` existing at the same time. Say you have
two posts in two categories, p1/c1 and p2/c2. Now do this:
{{{
$q = new WP_Query( array( 'p' => $p1 ) );
while ( $q->have_posts() ) {
$q->the_post();
$nested_q = new WP_Query( array( 'p' => $p2 ) );
while ( $nested_q->have_posts() ) {
$nested_q->have_posts();
get_term_meta( $c2, 'foo' );
}
}
}}}
That call to `get_term_meta()` should only prime the cache for the terms
belonging to `$nested_q` - ie, $c2 - and not those belonging to the parent
query - ie, $c1. (If you're not convinced by the nested queries, imagine a
dozen queries in serial.)
Your approach could be adapted, by indexing post IDs in the
`WP_Lazy_Loader` object by an identifier that is unique to the query
instance. Something like:
{{{
...
if ( $q['update_post_term_cache'] ) {
handle_lazy_loads( $this );
}
...
class WP_Lazy Loader {
...
function add_posts( WP_Query $query ) {
$key = _wp_filter_build_unique_id( array( $query, '' ) );
$this->queries[ $key ] = $posts;
}
....
function lazyload_term_meta( $check, $term_id ) {
...
foreach ( $this->queries as $qposts ) {
// If term belongs to one of the posts, then prime cache for
all post terms in the query
}
...
}
}}}
Does this seem right to you?
--
Ticket URL: <https://core.trac.wordpress.org/ticket/35816#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list