Make WordPress Core


Ignore:
Timestamp:
09/24/2012 08:35:56 PM (14 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r21967 r21981  
    497497
    498498                if ( 'post_category' == $key ) {
    499                         if ( is_object_in_taxonomy( $this->post_type, 'category' ) )
    500                                 return wp_get_post_categories( $this->ID );
    501                         else
     499                        $terms = get_the_terms( $this, 'category' );
     500                        if ( ! $terms )
    502501                                return array();
     502
     503                        return wp_list_pluck( $terms, 'term_id' );
    503504                }
    504505
    505506                if ( 'tags_input' == $key ) {
    506                         if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) )
    507                                 return wp_get_post_tags( $this->ID, array( 'fields' => 'names' ) );
    508                         else
     507                        $terms = get_the_terms( $this, 'post_tag' );
     508                        if ( ! $terms )
    509509                                return array();
     510
     511                        return wp_list_pluck( $terms, 'name' );
    510512                }
    511513
    … …  
    30423044        // names but different parents aren't confused.
    30433045        if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    3044                 $tags = array_map( 'intval', $tags );
    3045                 $tags = array_unique( $tags );
    3046         }
    3047 
    3048         return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
     3046                $tags = array_unique( array_map( 'intval', $tags ) );
     3047        }
     3048
     3049        $r = wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
     3050        if ( is_wp_error( $r ) )
     3051                return $r;
     3052
     3053        wp_cache_delete( $post_id, $taxonomy . '_relationships' );
     3054
     3055        return $r;
    30493056}
    30503057
    … …  
    30753082        }
    30763083
    3077         if ( !empty($post_categories) ) {
    3078                 $post_categories = array_map('intval', $post_categories);
    3079                 $post_categories = array_unique($post_categories);
    3080         }
    3081 
    3082         return wp_set_object_terms($post_ID, $post_categories, 'category');
     3084        return wp_set_post_terms($post_ID, $post_categories, 'category');
    30833085}
    30843086
Note: See TracChangeset for help on using the changeset viewer.