[wp-hackers] get_post() not fetching from post cache after update_post_caches()

William Canino william.canino at googlemail.com
Fri Oct 2 18:18:21 UTC 2009


Thank you for having a look.  I have isolated the issue. This code

function my_test_function() {
	global $wpdb, $wp_object_cache;
	$comments = $wpdb->get_results("Some query on wp_comments LIMIT 18"); 	
	if ( $comments ) : foreach ($comments as $comment) :		
		$post_in[] = $comment->post_ID;	
	endforeach; endif;	
	echo count($wp_object_cache->cache['posts'])."/".$wp_object_cache->cache_misses;
// Prints 21/68
	$my_query = new WP_Query(array('post__in' => $post_in));
	echo count($wp_object_cache->cache['posts'])."/".$wp_object_cache->cache_misses;
// Prints 27/88
	if ( $comments ) : foreach ($comments as $comment) :
		$title = get_the_title($comment->post_ID);
		$perma = get_permalink($comment->post_ID);
	endforeach; endif;
	echo count($wp_object_cache->cache['posts'])."/".$wp_object_cache->cache_misses;
// Prints 35/96
}

is producing the following $wpdb->queries dump. WordPress wizards, I
need help figuring out why 8 of the posts weren't cached. They are all
ordinary, published posts:

   [30] => Array
        (
            [0] =>  SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM
wp_posts  WHERE 1=1  AND wp_posts.ID IN
(1704,1714,1724,1705,1730,470,67,1016,1714,1720,1701,1640,988,1727,1718,1726,1724,1704)
AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private')  ORDER BY wp_posts.post_date DESC
LIMIT 0, 6
            [1] => 0.00011420249939
            [2] => require, require_once, include, my_test_function,
WP_Query->WP_Query, WP_Query->query, WP_Query->get_posts
        )

    [31] => Array
        (
            [0] => SELECT FOUND_ROWS()
            [1] => 6.00814819336E-5
            [2] => require, require_once, include, my_test_function,
WP_Query->WP_Query, WP_Query->query, WP_Query->get_posts
        )

    [32] => Array
        (
            [0] => SELECT t.*, tt.*, tr.object_id FROM wp_terms AS t
INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN
wp_term_relationships AS tr ON tr.term_taxonomy_id =
tt.term_taxonomy_id WHERE tt.taxonomy IN ('category', 'post_tag') AND
tr.object_id IN (1730, 1714, 1727, 1726, 1724, 1704) ORDER BY t.name
ASC
            [1] => 5.00679016113E-5
            [2] => require, require_once, include, my_test_function,
WP_Query->WP_Query, WP_Query->query, WP_Query->get_posts,
update_post_caches, update_object_term_cache, wp_get_object_terms
        )

    [33] => Array
        (
            [0] => SELECT post_id, meta_key, meta_value FROM
wp_postmeta WHERE post_id IN (1730,1714,1727,1726,1724,1704)
            [1] => 3.91006469727E-5
            [2] => require, require_once, include, my_test_function,
WP_Query->WP_Query, WP_Query->query, WP_Query->get_posts,
update_post_caches, update_postmeta_cache
        )

    [34] => Array
        (
            [0] => SELECT * FROM wp_posts WHERE ID = 1705 LIMIT 1
            [1] => 5.00679016113E-5
            [2] => require, require_once, include, my_test_function,
get_the_title, get_post
        )

    [35] => Array
        (
            [0] => SELECT * FROM wp_posts WHERE ID = 67 LIMIT 1
            [1] => 5.50746917725E-5
            [2] => require, require_once, include, my_test_function,
get_the_title, get_post
        )

followed by six others in the form of "SELECT * FROM wp_posts WHERE ID
= x LIMIT 1"

2009/10/2 Otto <otto at ottodestruct.com>:
> You shouldn't need to call update_post_caches, as the
> WP_Query->query() function does that automatically anyway. query.php,
> line 2359 (or thereabouts).
>
> As for the rest, I don't know. The get_post function should indeed
> check the cache first.
>
> Call global $wp_object_cache; $wp_object_cache->stats(); to see the
> contents of the cache before and after each call. Might help you with
> debugging.
>
> -Otto
> Sent from Memphis, TN, United States
>
>
> On Thu, Oct 1, 2009 at 11:47 AM, William Canino
> <william.canino at googlemail.com> wrote:
>> I'm coding a Yet-Another-Clickable-List-of-Posts-With-Something-in-Common.
>>  The amount of SQL queries my current code is making is driving me
>> nuts.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list