WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 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 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)

comment:1 @scribu4 years ago

  • Description modified (diff)

comment:2 @scribu4 years ago

  • Keywords reporter-feedback added; terms cache queries removed

Could you provide some steps to reproduce?

comment:3 @badskills4 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 4 years ago by badskills (previous) (diff)

comment:4 @badskills4 years ago

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

comment:5 @badskills4 years ago

  • Keywords reporter-feedback-added removed

comment:6 @scribu4 years ago

Thanks, that helps indeed.

comment:7 @scribu4 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.

comment:8 @westi4 years ago

  • Milestone changed from Awaiting Review to 3.1

Need to investigate for 3.1

comment:9 @badskills4 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.

comment:10 @scribu4 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

The correct resolution is 'invalid'.

comment:11 @scribu4 years ago

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