[wp-trac] [WordPress Trac] #14329: current_page_ancestor not being set because $post->ancestors is not being cached
WordPress Trac
wp-trac at lists.automattic.com
Fri Jul 16 13:47:35 UTC 2010
#14329: current_page_ancestor not being set because $post->ancestors is not being
cached
--------------------------+-------------------------------------------------
Reporter: layotte | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Cache | Version: 3.0
Severity: normal | Keywords: current_page_ancestor, $post->ancestors
--------------------------+-------------------------------------------------
I noticed a weird problem where current_page_ancestor class was not being
attached to the appropriate <li> from a wp_list_pages on a WPMS site.
I was running this wp_list_pages query:
$children =
wp_list_pages("title_li=&child_of=".$ugce."&echo=0&depth=1&sort_column=menu_order");
//$ugce is a dynamic parent ID
the current_page_item and current_page_parent classes are being set.
However, when I go to a grandchild, the current_page_ancestor is not being
set.
I looked through core and found in classes.php (line 1192 - WP3.0) is
calling $_current_page = get_page($current_page) and testing if
$_current_page->ancestors is set. It isn't set (for some reason), so it
skips to the next test.
I went over to post.php to where get_page merely calls get_post, and
noticed that at line 337, get_posts checks to see if the $post_id is
already in the wp_cache:
if ( ! $_post = wp_cache_get($post_id, 'posts') )
I assume that the current post would be in the wp_cache, so it merely
returns the cached results.
That made me think that perhaps the cache does not have the ancestors in
it. So I ran a test to find out more.
I setup my theme to output the post ID, the post ancestors, the array from
get_post, the array from wp_cache_get and then I cleared the wp_cache and
ran the same output to see how it changed.
before the wp_cache_flush(), the get_post and wp_cache_get does not
contain the post->ancestors. After the wp_cache_flush() they both contain
the post->ancestors.
I suspect that there is a place where the post is being added to cache
without the ancestors and then later when the ancestors are being check
from the cache it returns as if there are none.
This is my php code:
{{{
<?php echo "<strong>Post ID: </strong>" . $post->ID; ?>
<?php echo '<br><br><strong>$post->ancestors: </strong>';
print_r($post->ancestors); ?>
<?php $page = get_page( $post->ID ); ?>
<?php echo "<br><br><strong>print_r get_page: </strong>"; print_r($page);
?>
<?php $_page = wp_cache_get($post->ID, 'posts'); ?>
<?php echo "<br><br><strong>print_r wp_cache_get: </strong>";
print_r($_page); ?>
<?php echo "<br><br><strong>wp_cache_flush</strong"; wp_cache_flush(); ?>
<?php echo "<br><br><strong>Post ID: </strong>" . $post->ID; ?>
<?php echo '<br><br><strong>$post->ancestors: </strong>';
print_r($post->ancestors); ?>
<?php $page = get_page( $post->ID ); ?>
<?php echo "<br><br><strong>print_r get_page: </strong>"; print_r($page);
?>
<?php $_page = wp_cache_get($post->ID, 'posts'); ?>
<?php echo "<br><br><strong>print_r wp_cache_get: </strong>";
print_r($_page); ?>
}}}
And this is the output for my given page:
{{{
Post ID: 222
$post->ancestors: Array ( [0] => 216 [1] => 21 [2] => 17 )
print_r get_page: stdClass Object ( [ID] => 222 [post_author] => 1
[post_date] => 2010-07-15 19:09:48 [post_date_gmt] => 2010-07-15 19:09:48
[post_content] => Ed.S. [post_title] => Ed.S. [post_excerpt] =>
[post_status] => publish [comment_status] => open [ping_status] => open
[post_password] => [post_name] => ed-s [to_ping] => [pinged] =>
[post_modified] => 2010-07-16 12:10:59 [post_modified_gmt] => 2010-07-16
12:10:59 [post_content_filtered] => [post_parent] => 216 [guid] =>
http://webdev.coe.uga.edu/lle/ [menu_order] => 0 [post_type] => page
[post_mime_type] => [comment_count] => 0 [filter] => raw )
print_r wp_cache_get: stdClass Object ( [ID] => 222 [post_author] => 1
[post_date] => 2010-07-15 19:09:48 [post_date_gmt] => 2010-07-15 19:09:48
[post_content] => Ed.S. [post_title] => Ed.S. [post_excerpt] =>
[post_status] => publish [comment_status] => open [ping_status] => open
[post_password] => [post_name] => ed-s [to_ping] => [pinged] =>
[post_modified] => 2010-07-16 12:10:59 [post_modified_gmt] => 2010-07-16
12:10:59 [post_content_filtered] => [post_parent] => 216 [guid] =>
http://webdev.coe.uga.edu/lle/ [menu_order] => 0 [post_type] => page
[post_mime_type] => [comment_count] => 0 [filter] => raw )
wp_cache_flush
Post ID: 222
$post->ancestors: Array ( [0] => 216 [1] => 21 [2] => 17 )
print_r get_page: stdClass Object ( [ID] => 222 [post_author] => 1
[post_date] => 2010-07-15 19:09:48 [post_date_gmt] => 2010-07-15 19:09:48
[post_content] => Ed.S. [post_title] => Ed.S. [post_excerpt] =>
[post_status] => publish [comment_status] => open [ping_status] => open
[post_password] => [post_name] => ed-s [to_ping] => [pinged] =>
[post_modified] => 2010-07-16 12:10:59 [post_modified_gmt] => 2010-07-16
12:10:59 [post_content_filtered] => [post_parent] => 216 [guid] =>
http://webdev.coe.uga.edu/lle/ [menu_order] => 0 [post_type] => page
[post_mime_type] => [comment_count] => 0 [ancestors] => Array ( [0] => 216
[1] => 21 [2] => 17 ) [filter] => raw )
print_r wp_cache_get: stdClass Object ( [ID] => 222 [post_author] => 1
[post_date] => 2010-07-15 19:09:48 [post_date_gmt] => 2010-07-15 19:09:48
[post_content] => Ed.S. [post_title] => Ed.S. [post_excerpt] =>
[post_status] => publish [comment_status] => open [ping_status] => open
[post_password] => [post_name] => ed-s [to_ping] => [pinged] =>
[post_modified] => 2010-07-16 12:10:59 [post_modified_gmt] => 2010-07-16
12:10:59 [post_content_filtered] => [post_parent] => 216 [guid] =>
http://webdev.coe.uga.edu/lle/ [menu_order] => 0 [post_type] => page
[post_mime_type] => [comment_count] => 0 [ancestors] => Array ( [0] => 216
[1] => 21 [2] => 17 ) [filter] => raw )
}}}
--
Ticket URL: <http://core.trac.wordpress.org/ticket/14329>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list