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.


Built from https://develop.svn.wordpress.org/trunk@28561


git-svn-id: http://core.svn.wordpress.org/trunk@28387 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2014-05-23 19:29:14 +00:00
parent f50d5233f3
commit 5b5b756edf
2 changed files with 27 additions and 13 deletions

View File

@ -234,20 +234,24 @@ function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
if ( !$post_id )
return false;
$tags = wp_get_post_terms($post_id, $taxonomy, array());
$terms = get_object_term_cache( $post_id, $taxonomy );
if ( false === $terms ) {
$terms = wp_get_object_terms( $post_id, $taxonomy );
wp_cache_add( $post_id, $terms, $taxonomy . '_relationships' );
}
if ( !$tags ) {
if ( ! $terms ) {
return false;
}
if ( is_wp_error($tags) ) {
return $tags;
if ( is_wp_error( $terms ) ) {
return $terms;
}
$tag_names = array();
foreach ( $tags as $tag ) {
$tag_names[] = $tag->name;
$term_names = array();
foreach ( $terms as $term ) {
$term_names[] = $term->name;
}
$tags_to_edit = esc_attr( join( ',', $tag_names ) );
$terms_to_edit = esc_attr( join( ',', $term_names ) );
/**
* Filter the comma-separated list of terms available to edit.
@ -256,12 +260,12 @@ function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
*
* @see get_terms_to_edit()
*
* @param array $tags_to_edit An array of terms.
* @param array $terms_to_edit An array of terms.
* @param string $taxonomy The taxonomy for which to retrieve terms. Default 'post_tag'.
*/
$tags_to_edit = apply_filters( 'terms_to_edit', $tags_to_edit, $taxonomy );
$terms_to_edit = apply_filters( 'terms_to_edit', $terms_to_edit, $taxonomy );
return $tags_to_edit;
return $terms_to_edit;
}
/**

View File

@ -364,11 +364,21 @@ function get_inline_data($post) {
$taxonomy = get_taxonomy( $taxonomy_name );
if ( $taxonomy->hierarchical && $taxonomy->show_ui ) {
echo '<div class="post_category" id="' . $taxonomy_name . '_' . $post->ID . '">'
. implode( ',', wp_get_object_terms( $post->ID, $taxonomy_name, array( 'fields' => 'ids' ) ) ) . '</div>';
$terms = get_object_term_cache( $post->ID, $taxonomy_name );
if ( false === $terms ) {
$terms = wp_get_object_terms( $post->ID, $taxonomy_name );
wp_cache_add( $post->ID, $terms, $taxonomy_name . '_relationships' );
}
$term_ids = empty( $terms ) ? array() : wp_list_pluck( $terms, 'term_id' );
echo '<div class="post_category" id="' . $taxonomy_name . '_' . $post->ID . '">' . implode( ',', $term_ids ) . '</div>';
} elseif ( $taxonomy->show_ui ) {
echo '<div class="tags_input" id="'.$taxonomy_name.'_'.$post->ID.'">'
. esc_html( str_replace( ',', ', ', get_terms_to_edit( $post->ID, $taxonomy_name ) ) ) . '</div>';
}
}