Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#16253 closed defect (bug) (invalid)

Object term cache not working for taxonomy query page - resulting 200 queries for 10 posts

Reported by: badskills's profile badskills Owned by:
Milestone: Priority: normal
Severity: major Version: 3.0.4
Component: Cache API Keywords:
Focuses: Cc:

Description (last modified by scribu)

This problem is very similar to #12989
but now it happen when you query a taxonomy,

$terms = get_object_term_cache( $id, $taxonomy );
in get_the_terms() Return nothing

$link = get_term_link( $term, $taxonomy );
in get_the_term_list() also increase queries 

i am not that lucky with wordpress(Coding) to provide a patch but it took me lots of time to figure out why its happening.

Change History (11)

#1 @scribu
12 years ago

  • Description modified (diff)

#2 @scribu
12 years ago

  • Keywords reporter-feedback added; terms cache queries removed

Could you provide some steps to reproduce?

#3 @badskills
12 years ago

yes, just create a taxonomy for example fruits, then create a term in it eg Apple then attach 10 posts to term apple,
Add this in your loop

echo get_the_term_list( $post->ID, 'fruits', '--', ' » ',"--" );

then open http://localhost/wp/?fruits=apple, offcourse you are going to print queries at somewhere in bottom or degub/save all queries

for better claim just copy the get_the_term_list function to your theme function file and rename it like then call it and see if get_object_term_cache( $id, $taxonomy ); returns something or not ( a bit dirty method )

i expanded it little bit

function get_the_term_list_2( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
	//~ $terms = get_the_terms( $id, $taxonomy );
	//~ lets expand get_the_terms()
	
	
	 global $post;
	        //~ print_r2($post);
			$id = (int) $id;
			if ( !$id ) 
			{
				if ( !$post->id )
				return false;
				else
				$id = (int) $post->id;  
	        }
	        
			$terms = get_object_term_cache( $id, $taxonomy );

			// check here if you got something
                        // just print_r($terms);  // uncomment this

			if ( false === $terms )
			$terms = wp_get_object_terms( $id, $taxonomy );
			
			

	if ( is_wp_error( $terms ) )
		return $terms;

	if ( empty( $terms ) )
		return false;

	foreach ( $terms as $term ) {
		$link = get_term_link( $term, $taxonomy );
		if ( is_wp_error( $link ) )
			return $link;
		$term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
	}

	$term_links = apply_filters( "term_links-$taxonomy", $term_links );

	return $before . join( $sep, $term_links ) . $after;
}

	       

i hope it helps, the expanded function works great for homepage. now if you increase the terms attached to post it will increase queries.

Last edited 12 years ago by badskills (previous) (diff)

#4 @badskills
12 years ago

  • Keywords reporter-feedback-added added; reporter-feedback removed

#5 @badskills
12 years ago

  • Keywords reporter-feedback-added removed

#6 @scribu
12 years ago

Thanks, that helps indeed.

#7 @scribu
12 years ago

I can't seem to reproduce.

Here is the code I'm using, on a clean install of Twentyten:

<?php

add_action('init', function() {
	register_taxonomy('color', 'post', array('label' => 'Color'));
});

add_action('the_post', function( $post ) {
	print_r( get_object_term_cache( $post->ID, 'color' ) );
	echo the_terms( $post->ID, 'color' );
});

When going to ?color=green, the cache is pre-filled by WP_Query.

#8 @westi
12 years ago

  • Milestone changed from Awaiting Review to 3.1

Need to investigate for 3.1

#9 @badskills
12 years ago

  • Resolution set to fixed
  • Status changed from new to closed

i just completed testing with build 3.1-RC2-17315, and i found that there is no bug is latest build. taxonomy page comes in 20 queries (60 above in 3.0.4)

so closing the ticket.

#10 @scribu
12 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

The correct resolution is 'invalid'.

#11 @scribu
12 years ago

  • Milestone 3.1 deleted
  • Resolution set to invalid
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.