get_pages filter may be applied twice
|Reported by:||DD32||Owned by:||markjaquith|
This is a bit hard to explain; so hang in there while reading.
Under certain conditions(WP Object cache being enabled), the get_pages filter is applied twice:
First, We get the pages from the database, And filter it.
$pages = $wpdb->get_results($query); $pages = apply_filters('get_pages', $pages, $r);
Thats fine, Next, We locate any children, and cache it:
if ( $child_of || $hierarchical ) $pages = & get_page_children($child_of, $pages); $cache[ $key ] = $pages; wp_cache_set( 'get_pages', $cache, 'page' );
Thats also fine, So we've got the results, we've filtered it, and now we've put it into the cache as filtered.
Now, Next time the function runs:
$key = md5( serialize( $r ) ); if ( $cache = wp_cache_get( 'get_pages', 'page' ) ) if ( isset( $cache[ $key ] ) ) return apply_filters('get_pages', $cache[ $key ], $r );
We pull the cached _filtered_ results, and then we filter it again.
In general this isnt an issue, But if you've got a plugin adding pages to the get_pages function, AND the cache is enabled, then it ends up that the page will be added twice, Once when pulling from the DB, And then it'll be added again when its pulled from the cache later.
I've attached a patch to remove the filtering upon a cache retrieval, This has the downside that any plugin which modifies the get_pages output will have to deal with its output being cached.
I've also attached a patch to update the cache before filtering, So the filtering allways takes place.
Change History (10)
8 years ago
- Keywords get_pages added
- Milestone set to 2.3 (trunk)
- Owner changed from anonymous to markjaquith
- Status changed from new to assigned