Fetch full terms for the post_category and tags_input queries and then wp_list_pluck() the desired fields. Fetching full terms primes the cache and reduces overall queries. Add cache invalidation to wp_set_post_terms(). Props scribu. see #21309

git-svn-id: http://core.svn.wordpress.org/trunk@21981 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ryan Boren 2012-09-24 20:35:56 +00:00
parent 1924ec4117
commit 70edf4111b
2 changed files with 20 additions and 15 deletions

View File

@ -1062,6 +1062,9 @@ function get_the_terms( $post, $taxonomy ) {
if ( ! $post = get_post( $post ) ) if ( ! $post = get_post( $post ) )
return false; return false;
if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
return false;
$terms = get_object_term_cache( $post->ID, $taxonomy ); $terms = get_object_term_cache( $post->ID, $taxonomy );
if ( false === $terms ) { if ( false === $terms ) {
$terms = wp_get_object_terms( $post->ID, $taxonomy ); $terms = wp_get_object_terms( $post->ID, $taxonomy );

View File

@ -496,17 +496,19 @@ final class WP_Post {
} }
if ( 'post_category' == $key ) { if ( 'post_category' == $key ) {
if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) $terms = get_the_terms( $this, 'category' );
return wp_get_post_categories( $this->ID ); if ( ! $terms )
else
return array(); return array();
return wp_list_pluck( $terms, 'term_id' );
} }
if ( 'tags_input' == $key ) { if ( 'tags_input' == $key ) {
if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) $terms = get_the_terms( $this, 'post_tag' );
return wp_get_post_tags( $this->ID, array( 'fields' => 'names' ) ); if ( ! $terms )
else
return array(); return array();
return wp_list_pluck( $terms, 'name' );
} }
// Rest of the values need filtering // Rest of the values need filtering
@ -3041,11 +3043,16 @@ function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $a
// Hierarchical taxonomies must always pass IDs rather than names so that children with the same // Hierarchical taxonomies must always pass IDs rather than names so that children with the same
// names but different parents aren't confused. // names but different parents aren't confused.
if ( is_taxonomy_hierarchical( $taxonomy ) ) { if ( is_taxonomy_hierarchical( $taxonomy ) ) {
$tags = array_map( 'intval', $tags ); $tags = array_unique( array_map( 'intval', $tags ) );
$tags = array_unique( $tags );
} }
return wp_set_object_terms($post_id, $tags, $taxonomy, $append); $r = wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
if ( is_wp_error( $r ) )
return $r;
wp_cache_delete( $post_id, $taxonomy . '_relationships' );
return $r;
} }
/** /**
@ -3074,12 +3081,7 @@ function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
return true; return true;
} }
if ( !empty($post_categories) ) { return wp_set_post_terms($post_ID, $post_categories, 'category');
$post_categories = array_map('intval', $post_categories);
$post_categories = array_unique($post_categories);
}
return wp_set_object_terms($post_ID, $post_categories, 'category');
} }
/** /**