From 70edf4111bc264eac936c95eddbbc720eca2c1bc Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Mon, 24 Sep 2012 20:35:56 +0000 Subject: [PATCH] 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 --- wp-includes/category-template.php | 3 +++ wp-includes/post.php | 32 ++++++++++++++++--------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index c4ba37d0d9..ce29c90c47 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -1062,6 +1062,9 @@ function get_the_terms( $post, $taxonomy ) { if ( ! $post = get_post( $post ) ) return false; + if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) + return false; + $terms = get_object_term_cache( $post->ID, $taxonomy ); if ( false === $terms ) { $terms = wp_get_object_terms( $post->ID, $taxonomy ); diff --git a/wp-includes/post.php b/wp-includes/post.php index fcac8e7da3..ddf78ab625 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -496,17 +496,19 @@ final class WP_Post { } if ( 'post_category' == $key ) { - if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) - return wp_get_post_categories( $this->ID ); - else + $terms = get_the_terms( $this, 'category' ); + if ( ! $terms ) return array(); + + return wp_list_pluck( $terms, 'term_id' ); } if ( 'tags_input' == $key ) { - if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) - return wp_get_post_tags( $this->ID, array( 'fields' => 'names' ) ); - else + $terms = get_the_terms( $this, 'post_tag' ); + if ( ! $terms ) return array(); + + return wp_list_pluck( $terms, 'name' ); } // 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 // names but different parents aren't confused. if ( is_taxonomy_hierarchical( $taxonomy ) ) { - $tags = array_map( 'intval', $tags ); - $tags = array_unique( $tags ); + $tags = array_unique( array_map( 'intval', $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; } - if ( !empty($post_categories) ) { - $post_categories = array_map('intval', $post_categories); - $post_categories = array_unique($post_categories); - } - - return wp_set_object_terms($post_ID, $post_categories, 'category'); + return wp_set_post_terms($post_ID, $post_categories, 'category'); } /**