Make WordPress Core

Changeset 28561


Ignore:
Timestamp:
05/23/2014 07:28:45 PM (11 years ago)
Author:
wonderboymusic
Message:

In get_terms_to_edit(), call get_object_term_cache() before priming cache with wp_get_object_terms().

In get_inline_data(), do the same.

On list table screens with taxonomies in the admin, this dramatically reduces the number of database queries (3x less). Even more so with a persistent object cache (5x less).

Props johnbillion, jeffstieler, wonderboymusic.
Fixes #18968.

Location:
trunk/src/wp-admin/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/taxonomy.php

    r28500 r28561  
    235235        return false;
    236236
    237     $tags = wp_get_post_terms($post_id, $taxonomy, array());
    238 
    239     if ( !$tags ) {
     237    $terms = get_object_term_cache( $post_id, $taxonomy );
     238    if ( false === $terms ) {
     239        $terms = wp_get_object_terms( $post_id, $taxonomy );
     240        wp_cache_add( $post_id, $terms, $taxonomy . '_relationships' );
     241    }
     242
     243    if ( ! $terms ) {
    240244        return false;
    241245    }
    242     if ( is_wp_error($tags) ) {
    243         return $tags;
    244     }
    245     $tag_names = array();
    246     foreach ( $tags as $tag ) {
    247         $tag_names[] = $tag->name;
    248     }
    249 
    250     $tags_to_edit = esc_attr( join( ',', $tag_names ) );
     246    if ( is_wp_error( $terms ) ) {
     247        return $terms;
     248    }
     249    $term_names = array();
     250    foreach ( $terms as $term ) {
     251        $term_names[] = $term->name;
     252    }
     253
     254    $terms_to_edit = esc_attr( join( ',', $term_names ) );
    251255
    252256    /**
     
    257261     * @see get_terms_to_edit()
    258262     *
    259      * @param array  $tags_to_edit An array of terms.
     263     * @param array  $terms_to_edit An array of terms.
    260264     * @param string $taxonomy     The taxonomy for which to retrieve terms. Default 'post_tag'.
    261265     */
    262     $tags_to_edit = apply_filters( 'terms_to_edit', $tags_to_edit, $taxonomy );
    263 
    264     return $tags_to_edit;
     266    $terms_to_edit = apply_filters( 'terms_to_edit', $terms_to_edit, $taxonomy );
     267
     268    return $terms_to_edit;
    265269}
    266270
  • trunk/src/wp-admin/includes/template.php

    r28514 r28561  
    365365
    366366        if ( $taxonomy->hierarchical && $taxonomy->show_ui ) {
    367                 echo '<div class="post_category" id="' . $taxonomy_name . '_' . $post->ID . '">'
    368                     . implode( ',', wp_get_object_terms( $post->ID, $taxonomy_name, array( 'fields' => 'ids' ) ) ) . '</div>';
     367
     368            $terms = get_object_term_cache( $post->ID, $taxonomy_name );
     369            if ( false === $terms ) {
     370                $terms = wp_get_object_terms( $post->ID, $taxonomy_name );
     371                wp_cache_add( $post->ID, $terms, $taxonomy_name . '_relationships' );
     372            }
     373            $term_ids = empty( $terms ) ? array() : wp_list_pluck( $terms, 'term_id' );
     374
     375            echo '<div class="post_category" id="' . $taxonomy_name . '_' . $post->ID . '">' . implode( ',', $term_ids ) . '</div>';
     376
    369377        } elseif ( $taxonomy->show_ui ) {
     378
    370379            echo '<div class="tags_input" id="'.$taxonomy_name.'_'.$post->ID.'">'
    371380                . esc_html( str_replace( ',', ', ', get_terms_to_edit( $post->ID, $taxonomy_name ) ) ) . '</div>';
     381
    372382        }
    373383    }
Note: See TracChangeset for help on using the changeset viewer.