<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[34529] trunk: Introduce metadata for taxonomy terms.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/34529">34529</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/34529","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-09-25 03:58:59 +0000 (Fri, 25 Sep 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Introduce metadata for taxonomy terms.

Adds a new table to the database schema (`wp_termmeta`), and a set of
`*_term_meta()` API functions. `get_terms()` and `wp_get_object_terms()`
now also support 'meta_query' parameters, with syntax identical to other
uses of `WP_Meta_Query`.

When fetching terms via `get_terms()` or `wp_get_object_terms()`, metadata for
matched terms is preloaded into the cache by default. Disable this behavior
by setting the new `$update_term_meta_cache` paramater to `false`.

To maximize performance, within `WP_Query` loops, the termmeta cache is *not*
primed by default. Instead, we use a lazy-loading technique: metadata for all
terms belonging to posts in the loop is loaded into the cache the first time
that `get_term_meta()` is called within the loop.

Props boonebgorges, sirzooro.
See <a href="https://core.trac.wordpress.org/ticket/10142">#10142</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesschemaphp">trunk/src/wp-admin/includes/schema.php</a></li>
<li><a href="#trunksrcwpadminincludesupgradephp">trunk/src/wp-admin/includes/upgrade.php</a></li>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludestaxonomyfunctionsphp">trunk/src/wp-includes/taxonomy-functions.php</a></li>
<li><a href="#trunksrcwpincludesversionphp">trunk/src/wp-includes/version.php</a></li>
<li><a href="#trunksrcwpincludeswpdbphp">trunk/src/wp-includes/wp-db.php</a></li>
<li><a href="#trunktestsphpunitteststermgetTermsphp">trunk/tests/phpunit/tests/term/getTerms.php</a></li>
<li><a href="#trunktestsphpunitteststermwpGetObjectTermsphp">trunk/tests/phpunit/tests/term/wpGetObjectTerms.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunitteststermmetaphp">trunk/tests/phpunit/tests/term/meta.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesschemaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/schema.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/schema.php    2015-09-25 02:37:29 UTC (rev 34528)
+++ trunk/src/wp-admin/includes/schema.php      2015-09-25 03:58:59 UTC (rev 34529)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -57,7 +57,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $max_index_length = 191;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // Blog specific tables.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $blog_tables = "CREATE TABLE $wpdb->terms (
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $blog_tables = "CREATE TABLE $wpdb->termmeta (
+  meta_id bigint(20) unsigned NOT NULL auto_increment,
+  term_id bigint(20) unsigned NOT NULL default '0',
+  meta_key varchar(255) default NULL,
+  meta_value longtext,
+  PRIMARY KEY (meta_id),
+  KEY term_id (term_id),
+  KEY meta_key (meta_key($max_index_length))
+) $charset_collate;
+CREATE TABLE $wpdb->terms (
</ins><span class="cx" style="display: block; padding: 0 10px">  term_id bigint(20) unsigned NOT NULL auto_increment,
</span><span class="cx" style="display: block; padding: 0 10px">  name varchar(200) NOT NULL default '',
</span><span class="cx" style="display: block; padding: 0 10px">  slug varchar(200) NOT NULL default '',
</span></span></pre></div>
<a id="trunksrcwpadminincludesupgradephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/upgrade.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/upgrade.php   2015-09-25 02:37:29 UTC (rev 34528)
+++ trunk/src/wp-admin/includes/upgrade.php     2015-09-25 03:58:59 UTC (rev 34529)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2628,6 +2628,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $wpdb->query( "ALTER TABLE $wpdb->postmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
</span><span class="cx" style="display: block; padding: 0 10px">                $wpdb->query( "ALTER TABLE $wpdb->posts DROP INDEX post_name, ADD INDEX post_name(post_name(191))" );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       // Upgrade versions prior to 4.4.
+       if ( $wp_current_db_version < 34370 ) {
+               // If compatible termmeta table is found, use it, but enforce a proper index.
+               if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->termmeta}'" ) && $wpdb->get_results( "SHOW INDEX FROM {$wpdb->termmeta} WHERE Column_name = 'meta_key'" ) ) {
+                       $wpdb->query( "ALTER TABLE $wpdb->termmeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
+               }
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2015-09-25 02:37:29 UTC (rev 34528)
+++ trunk/src/wp-includes/default-filters.php   2015-09-25 03:58:59 UTC (rev 34529)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -201,6 +201,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'xmlrpc_pingback_error',    'xmlrpc_pingback_error'               );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'title_save_pre',           'trim'                                );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'get_comment_metadata',     'wp_lazyload_comment_meta',     10, 2 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_filter( 'get_term_metadata',        'wp_lazyload_term_meta',        10, 2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludestaxonomyfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/taxonomy-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy-functions.php      2015-09-25 02:37:29 UTC (rev 34528)
+++ trunk/src/wp-includes/taxonomy-functions.php        2015-09-25 03:58:59 UTC (rev 34529)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -957,6 +957,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.3.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.2.0 Introduced 'name' and 'childless' parameters.
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.4.0 Introduced the ability to pass 'term_id' as an alias of 'id' for the `orderby` parameter.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *              Introduced the 'meta_query' and 'update_term_meta_cache' parameters.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global wpdb  $wpdb WordPress database abstraction object.
</span><span class="cx" style="display: block; padding: 0 10px">  * @global array $wp_filter
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1013,6 +1014,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                                           no effect on non-hierarchical taxonomies. Default false.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type string       $cache_domain      Unique cache key to be produced when this query is stored in an
</span><span class="cx" style="display: block; padding: 0 10px">  *                                           object cache. Default is 'core'.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *     @type bool         $update_term_meta_cache Whether to prime meta caches for matched terms. Default true.
+ *     @type array        $meta_query             Meta query clauses to limit retrieved terms by.
+ *                                                See `WP_Meta_Query`. Default empty.
</ins><span class="cx" style="display: block; padding: 0 10px">  * }
</span><span class="cx" style="display: block; padding: 0 10px">  * @return array|int|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies
</span><span class="cx" style="display: block; padding: 0 10px">  *                        do not exist.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1036,7 +1040,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 'childless' => false,
</span><span class="cx" style="display: block; padding: 0 10px">                'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core',
+               'update_term_meta_cache' => true, 'meta_query' => '' );
</ins><span class="cx" style="display: block; padding: 0 10px">         $args = wp_parse_args( $args, $defaults );
</span><span class="cx" style="display: block; padding: 0 10px">        $args['number'] = absint( $args['number'] );
</span><span class="cx" style="display: block; padding: 0 10px">        $args['offset'] = absint( $args['offset'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1296,6 +1301,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Meta query support.
+       $join = '';
+       if ( ! empty( $args['meta_query'] ) ) {
+               $mquery = new WP_Meta_Query( $args['meta_query'] );
+               $mq_sql = $mquery->get_sql( 'term', 't', 'term_id' );
+
+               $join  .= $mq_sql['join'];
+               $where .= $mq_sql['where'];
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $selects = array();
</span><span class="cx" style="display: block; padding: 0 10px">        switch ( $args['fields'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                case 'all':
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1341,7 +1356,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $join .= " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1374,6 +1389,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                update_term_cache( $terms );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Prime termmeta cache.
+       if ( $args['update_term_meta_cache'] ) {
+               $term_ids = wp_list_pluck( $terms, 'term_id' );
+               update_termmeta_cache( $term_ids );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( empty($terms) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1455,6 +1476,142 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Adds metadata to a term.
+ *
+ * @since 4.4.0
+ *
+ * @param int    $term_id    Term ID.
+ * @param string $meta_key   Metadata name.
+ * @param mixed  $meta_value Metadata value.
+ * @param bool   $unique     Optional. Whether to bail if an entry with the same key is found for the term.
+ *                           Default false.
+ * @return int|bool Meta ID on success, false on failure.
+ */
+function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
+       return add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
+}
+
+/**
+ * Removes metadata matching criteria from a term.
+ *
+ * @since 4.4.0
+ *
+ * @param int    $term_id    Term ID.
+ * @param string $meta_key   Metadata name.
+ * @param mixed  $meta_value Optional. Metadata value. If provided, rows will only be removed that match the value.
+ * @return bool True on success, false on failure.
+ */
+function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
+       return delete_metadata( 'term', $term_id, $meta_key, $meta_value );
+}
+
+/**
+ * Retrieves metadata for a term.
+ *
+ * @since 4.4.0
+ *
+ * @param int    $term_id Term ID.
+ * @param string $key     Optional. The meta key to retrieve. If no key is provided, fetches all metadata for the term.
+ * @param bool   $single  Whether to return a single value. If false, an array of all values matching the
+ *                        `$term_id`/`$key` pair will be returned. Default: false.
+ * @return mixed If `$single` is false, an array of metadata values. If `$single` is true, a single metadata value.
+ */
+function get_term_meta( $term_id, $key = '', $single = false ) {
+       return get_metadata( 'term', $term_id, $key, $single );
+}
+
+/**
+ * Updates term metadata.
+ *
+ * Use the `$prev_value` parameter to differentiate between meta fields with the same key and term ID.
+ *
+ * If the meta field for the term does not exist, it will be added.
+ *
+ * @since 4.4.0
+ *
+ * @param int    $term_id    Term ID.
+ * @param string $meta_key   Metadata key.
+ * @param mixed  $meta_value Metadata value.
+ * @param mixed  $prev_value Optional. Previous value to check before removing.
+ * @return int|bool Meta ID if the key didn't previously exist. True on successful update. False on failure.
+ */
+function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
+       return update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
+}
+
+/**
+ * Updates metadata cache for list of term IDs.
+ *
+ * Performs SQL query to retrieve all metadata for the terms matching `$term_ids` and stores them in the cache.
+ * Subsequent calls to `get_term_meta()` will not need to query the database.
+ *
+ * @since 4.4.0
+ *
+ * @param array $term_ids List of term IDs.
+ * @return array|false Returns false if there is nothing to update. Returns an array of metadata on success.
+ */
+function update_termmeta_cache( $term_ids ) {
+       return update_meta_cache( 'term', $term_ids );
+}
+
+/**
+ * Lazy-loads termmeta when inside of a `WP_Query` loop.
+ *
+ * As a rule, term queries (`get_terms()` and `wp_get_object_terms()`) prime the metadata cache for matched terms by
+ * default. However, this can cause a slight performance penalty, especially when that metadata is not actually used.
+ * In the context of a `WP_Query` loop, we're able to avoid this potential penalty. `update_object_term_cache()`,
+ * called from `update_post_caches()`, does not 'update_term_meta_cache'. Instead, the first time `get_term_meta()` is
+ * called from within a `WP_Query` loop, the current function detects the fact, and then primes the metadata cache for
+ * all terms attached to all posts in the loop, with a single database query.
+ *
+ * @since 4.4.0
+ *
+ * @param null $check   The `$check` param passed from the 'pre_term_metadata' hook.
+ * @param int  $term_id ID of the term whose metadata is being cached.
+ * @return null In order not to short-circuit `get_metadata()`.
+ */
+function wp_lazyload_term_meta( $check, $term_id ) {
+       global $wp_query;
+
+       if ( $wp_query instanceof WP_Query && ! empty( $wp_query->posts ) && $wp_query->get( 'update_post_term_cache' ) ) {
+               // We can only lazyload if the entire post object is present.
+               $posts = array();
+               foreach ( $wp_query->posts as $post ) {
+                       if ( $post instanceof WP_Post ) {
+                               $posts[] = $post;
+                       }
+               }
+
+               if ( empty( $posts ) ) {
+                       return;
+               }
+
+               // Fetch cached term_ids for each post. Keyed by term_id for faster lookup.
+               $term_ids = array();
+               foreach ( $posts as $post ) {
+                       $taxonomies = get_object_taxonomies( $post->post_type );
+                       foreach ( $taxonomies as $taxonomy ) {
+                               // No extra queries. Term cache should already be primed by 'update_post_term_cache'.
+                               $terms = get_object_term_cache( $post->ID, $taxonomy );
+                               if ( false !== $terms ) {
+                                       foreach ( $terms as $term ) {
+                                               if ( ! isset( $term_ids[ $term->term_id ] ) ) {
+                                                       $term_ids[ $term->term_id ] = 1;
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               if ( $term_ids ) {
+                       update_termmeta_cache( array_keys( $term_ids ) );
+               }
+       }
+
+       return $check;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Check if Term exists.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * Formerly is_term(), introduced in 2.3.0.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2011,6 +2168,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.3.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`.
</span><span class="cx" style="display: block; padding: 0 10px">  *              Introduced `$parent` argument.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global wpdb $wpdb WordPress database abstraction object.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2026,6 +2184,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                           term objects being returned, 'ids' will return an array of integers, and 'names' an array
</span><span class="cx" style="display: block; padding: 0 10px">  *                           of strings.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type int    $parent  Optional. Limit results to the direct children of a given term ID.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *     @type bool   $update_term_meta_cache Whether to prime termmeta cache for matched terms. Only applies when
+ *                                          `$fields` is 'all', 'all_with_object_id', or 'term_id'. Default true.
+ *     @type array  $meta_query             Meta query clauses to limit retrieved terms by. See `WP_Meta_Query`.
+ *                                          Default empty.
</ins><span class="cx" style="display: block; padding: 0 10px">  * }
</span><span class="cx" style="display: block; padding: 0 10px">  * @return array|WP_Error The requested term data or empty array if no terms found.
</span><span class="cx" style="display: block; padding: 0 10px">  *                        WP_Error if any of the $taxonomies don't exist.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2053,6 +2215,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'order'   => 'ASC',
</span><span class="cx" style="display: block; padding: 0 10px">                'fields'  => 'all',
</span><span class="cx" style="display: block; padding: 0 10px">                'parent'  => '',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                'update_term_meta_cache' => true,
+               'meta_query' => '',
</ins><span class="cx" style="display: block; padding: 0 10px">         );
</span><span class="cx" style="display: block; padding: 0 10px">        $args = wp_parse_args( $args, $defaults );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2126,9 +2290,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $where[] = $wpdb->prepare( 'tt.parent = %d', $args['parent'] );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Meta query support.
+       $meta_query_join = '';
+       if ( ! empty( $args['meta_query'] ) ) {
+               $mquery = new WP_Meta_Query( $args['meta_query'] );
+               $mq_sql = $mquery->get_sql( 'term', 't', 'term_id' );
+
+               $meta_query_join .= $mq_sql['join'];
+
+               // Strip leading AND.
+               $where[] = preg_replace( '/^\s*AND/', '', $mq_sql['where'] );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $where = implode( ' AND ', $where );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE $where $orderby $order";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id $meta_query_join WHERE $where $orderby $order";
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $objects = false;
</span><span class="cx" style="display: block; padding: 0 10px">        if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2153,6 +2329,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Update termmeta cache, if necessary.
+       if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_ids' === $fields || 'term_id' === $fields ) ) {
+               if ( 'term_id' === $fields ) {
+                       $term_ids = $fields;
+               } else {
+                       $term_ids = wp_list_pluck( $terms, 'term_id' );
+               }
+
+               update_termmeta_cache( $term_ids );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( ! $terms ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $terms = array();
</span><span class="cx" style="display: block; padding: 0 10px">        } elseif ( $objects && 'all_with_object_id' !== $fields ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3288,6 +3475,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $terms = wp_get_object_terms( $ids, $taxonomies, array(
</span><span class="cx" style="display: block; padding: 0 10px">                'fields' => 'all_with_object_id',
</span><span class="cx" style="display: block; padding: 0 10px">                'orderby' => 'none',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                'update_term_meta_cache' => false,
</ins><span class="cx" style="display: block; padding: 0 10px">         ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $object_terms = array();
</span></span></pre></div>
<a id="trunksrcwpincludesversionphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/version.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/version.php 2015-09-25 02:37:29 UTC (rev 34528)
+++ trunk/src/wp-includes/version.php   2015-09-25 03:58:59 UTC (rev 34529)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -11,7 +11,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global int $wp_db_version
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-$wp_db_version = 34030;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+$wp_db_version = 34528;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * Holds the TinyMCE version
</span></span></pre></div>
<a id="trunksrcwpincludeswpdbphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/wp-db.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/wp-db.php   2015-09-25 02:37:29 UTC (rev 34528)
+++ trunk/src/wp-includes/wp-db.php     2015-09-25 03:58:59 UTC (rev 34529)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -266,7 +266,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @var array
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         'terms', 'term_taxonomy', 'term_relationships', 'termmeta', 'commentmeta' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * List of deprecated WordPress tables
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -382,6 +382,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public $term_taxonomy;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * WordPress Term Meta table.
+        *
+        * @since 4.4.0
+        * @access public
+        * @var string
+        */
+       public $termmeta;
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /*
</span><span class="cx" style="display: block; padding: 0 10px">         * Global and Multisite tables
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span></span></pre></div>
<a id="trunktestsphpunitteststermgetTermsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/term/getTerms.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/getTerms.php       2015-09-25 02:37:29 UTC (rev 34528)
+++ trunk/tests/phpunit/tests/term/getTerms.php 2015-09-25 03:58:59 UTC (rev 34529)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,7 +22,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $num_queries = $wpdb->num_queries;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // last_changed and num_queries should bump
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $terms = get_terms( 'post_tag' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( 3, count( $terms ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $time1 = wp_cache_get( 'last_changed', 'terms' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNotEmpty( $time1 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -31,7 +31,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $num_queries = $wpdb->num_queries;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Again. last_changed and num_queries should remain the same.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $terms = get_terms( 'post_tag' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( 3, count( $terms ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $num_queries, $wpdb->num_queries );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1502,6 +1502,84 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @ticket 10142
+        */
+       public function test_termmeta_cache_should_be_primed_by_default() {
+               global $wpdb;
+
+               register_taxonomy( 'wptests_tax', 'post' );
+               $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $terms[0], 'foo', 'bar' );
+               add_term_meta( $terms[1], 'foo', 'bar' );
+               add_term_meta( $terms[2], 'foo', 'bar' );
+
+               $found = get_terms( 'wptests_tax', array(
+                       'hide_empty' => false,
+                       'include' => $terms,
+               ) );
+
+               $num_queries = $wpdb->num_queries;
+
+               foreach ( $terms as $t ) {
+                       $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
+               }
+
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+       }
+
+       /**
+        * @ticket 10142
+        */
+       public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() {
+               global $wpdb;
+
+               register_taxonomy( 'wptests_tax', 'post' );
+               $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $terms[0], 'foo', 'bar' );
+               add_term_meta( $terms[1], 'foo', 'bar' );
+               add_term_meta( $terms[2], 'foo', 'bar' );
+
+               $found = get_terms( 'wptests_tax', array(
+                       'hide_empty' => false,
+                       'include' => $terms,
+                       'update_term_meta_cache' => false,
+               ) );
+
+               $num_queries = $wpdb->num_queries;
+
+               foreach ( $terms as $t ) {
+                       $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
+               }
+
+               $this->assertSame( $num_queries + 3, $wpdb->num_queries );
+       }
+
+       /**
+        * @ticket 10142
+        */
+       public function test_meta_query() {
+               register_taxonomy( 'wptests_tax', 'post' );
+               $terms = $this->factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $terms[0], 'foo', 'bar' );
+               add_term_meta( $terms[1], 'foo', 'bar' );
+               add_term_meta( $terms[2], 'foo', 'baz' );
+               add_term_meta( $terms[3], 'foob', 'ar' );
+
+               $found = get_terms( 'wptests_tax', array(
+                       'hide_empty' => false,
+                       'meta_query' => array(
+                               array(
+                                       'key' => 'foo',
+                                       'value' => 'bar',
+                               ),
+                       ),
+                       'fields' => 'ids',
+               ) );
+
+               $this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         protected function create_hierarchical_terms_and_posts() {
</span><span class="cx" style="display: block; padding: 0 10px">                $terms = array();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunitteststermmetaphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/term/meta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/meta.php                           (rev 0)
+++ trunk/tests/phpunit/tests/term/meta.php     2015-09-25 03:58:59 UTC (rev 34529)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,147 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group taxonomy
+ * @group meta
+ * @ticket 10142
+ */
+class Tests_Term_Meta extends WP_UnitTestCase {
+       public function setUp() {
+               parent::setUp();
+               register_taxonomy( 'wptests_tax', 'post' );
+       }
+
+       public function test_add() {
+               $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+
+               $this->assertNotEmpty( add_term_meta( $t, 'foo', 'bar' ) );
+       }
+
+       public function test_add_unique() {
+               $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+
+               $this->assertNotEmpty( add_term_meta( $t, 'foo', 'bar' ) );
+               $this->assertFalse( add_term_meta( $t, 'foo', 'bar', true ) );
+       }
+
+       public function test_delete() {
+               $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $t, 'foo', 'bar' );
+
+               $this->assertTrue( delete_term_meta( $t, 'foo' ) );
+       }
+
+       public function test_delete_with_invalid_meta_key_should_return_false() {
+               $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+
+               $this->assertFalse( delete_term_meta( $t, 'foo' ) );
+       }
+
+       public function test_delete_should_respect_meta_value() {
+               $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $t, 'foo', 'bar' );
+               add_term_meta( $t, 'foo', 'baz' );
+
+               $this->assertTrue( delete_term_meta( $t, 'foo', 'bar' ) );
+
+               $metas = get_term_meta( $t, 'foo', false );
+               $this->assertSame( array( 'baz' ), $metas );
+       }
+
+       public function test_get_with_no_key_should_fetch_all_keys() {
+               $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $t, 'foo', 'bar' );
+               add_term_meta( $t, 'foo1', 'baz' );
+
+               $found = get_term_meta( $t );
+               $expected = array(
+                       'foo' => array( 'bar' ),
+                       'foo1' => array( 'baz' ),
+               );
+
+               $this->assertEqualSets( $expected, $found );
+       }
+
+       public function test_get_with_key_should_fetch_all_for_key() {
+               $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $t, 'foo', 'bar' );
+               add_term_meta( $t, 'foo', 'baz' );
+               add_term_meta( $t, 'foo1', 'baz' );
+
+               $found = get_term_meta( $t, 'foo' );
+               $expected = array( 'bar', 'baz' );
+
+               $this->assertEqualSets( $expected, $found );
+       }
+
+       public function test_get_should_respect_single_true() {
+               $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $t, 'foo', 'bar' );
+               add_term_meta( $t, 'foo', 'baz' );
+
+               $found = get_term_meta( $t, 'foo', true );
+               $this->assertEquals( 'bar', $found );
+       }
+
+       public function test_update_should_pass_to_add_when_no_value_exists_for_key() {
+               $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+
+               $actual = update_term_meta( $t, 'foo', 'bar' );
+               $this->assertInternalType( 'int', $actual );
+               $this->assertNotEmpty( $actual );
+
+               $meta = get_term_meta( $t, 'foo', true );
+               $this->assertSame( 'bar', $meta );
+       }
+
+       public function test_update_should_return_true_when_updating_existing_value_for_key() {
+               $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+
+               add_term_meta( $t, 'foo', 'bar' );
+
+               $actual = update_term_meta( $t, 'foo', 'baz' );
+               $this->assertTrue( $actual );
+
+               $meta = get_term_meta( $t, 'foo', true );
+               $this->assertSame( 'baz', $meta );
+       }
+
+       public function test_term_meta_should_be_lazy_loaded_for_all_terms_in_wp_query_loop() {
+               global $wpdb;
+
+               $p = $this->factory->post->create( array( 'post_status' => 'publish' ) );
+
+               register_taxonomy( 'wptests_tax', 'post' );
+               $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
+               wp_set_object_terms( $p, $terms, 'wptests_tax' );
+               foreach ( $terms as $t ) {
+                       add_term_meta( $t, 'foo', 'bar' );
+               }
+
+               // Create another term, which should *not* be lazy loaded because it's unattached.
+               $orphan_term = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $orphan_term, 'foo', 'bar' );
+
+               $this->go_to( get_permalink( $p ) );
+
+               if ( have_posts() ) {
+                       while ( have_posts() ) {
+                               the_post();
+
+                               // First request will hit the database.
+                               $num_queries = $wpdb->num_queries;
+                               $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
+                               $this->assertSame( $num_queries + 1, $wpdb->num_queries );
+
+                               // Second and third requests should be in cache.
+                               $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
+                               $this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) );
+                               $this->assertSame( $num_queries + 1, $wpdb->num_queries );
+
+                               // Querying a term not primed should result in a hit.
+                               $this->assertSame( 'bar', get_term_meta( $orphan_term, 'foo', true ) );
+                               $this->assertSame( $num_queries + 2, $wpdb->num_queries );
+                       }
+               }
+       }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitteststermwpGetObjectTermsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/term/wpGetObjectTerms.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/wpGetObjectTerms.php       2015-09-25 02:37:29 UTC (rev 34528)
+++ trunk/tests/phpunit/tests/term/wpGetObjectTerms.php 2015-09-25 03:58:59 UTC (rev 34529)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -418,6 +418,86 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEqualSets( array( $t1, $t2 ), $found );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @ticket 10142
+        */
+       public function test_termmeta_cache_should_be_primed_by_default() {
+               global $wpdb;
+
+               register_taxonomy( 'wptests_tax', 'post' );
+               $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $terms[0], 'foo', 'bar' );
+               add_term_meta( $terms[1], 'foo', 'bar' );
+               add_term_meta( $terms[2], 'foo', 'bar' );
+
+               $p = $this->factory->post->create();
+               wp_set_object_terms( $p, $terms, 'wptests_tax' );
+
+               $found = wp_get_object_terms( $p, 'wptests_tax' );
+
+               $num_queries = $wpdb->num_queries;
+
+               foreach ( $terms as $t ) {
+                       $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
+               }
+
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+       }
+
+       /**
+        * @ticket 10142
+        */
+       public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() {
+               global $wpdb;
+
+               register_taxonomy( 'wptests_tax', 'post' );
+               $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $terms[0], 'foo', 'bar' );
+               add_term_meta( $terms[1], 'foo', 'bar' );
+               add_term_meta( $terms[2], 'foo', 'bar' );
+
+               $p = $this->factory->post->create();
+               wp_set_object_terms( $p, $terms, 'wptests_tax' );
+
+               $found = wp_get_object_terms( $p, 'wptests_tax', array(
+                       'update_term_meta_cache' => false,
+               ) );
+
+               $num_queries = $wpdb->num_queries;
+
+               foreach ( $terms as $t ) {
+                       $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
+               }
+
+               $this->assertSame( $num_queries + 3, $wpdb->num_queries );
+       }
+
+       /**
+        * @ticket 10142
+        */
+       public function test_meta_query() {
+               register_taxonomy( 'wptests_tax', 'post' );
+               $terms = $this->factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
+               add_term_meta( $terms[0], 'foo', 'bar' );
+               add_term_meta( $terms[1], 'foo', 'bar' );
+               add_term_meta( $terms[2], 'foo', 'baz' );
+               add_term_meta( $terms[3], 'foob', 'ar' );
+
+               $p = $this->factory->post->create();
+               wp_set_object_terms( $p, $terms, 'wptests_tax' );
+
+               $found = wp_get_object_terms( $p, 'wptests_tax', array(
+                       'meta_query' => array(
+                               array(
+                                       'key' => 'foo',
+                                       'value' => 'bar',
+                               ),
+                       ),
+               ) );
+
+               $this->assertEqualSets( array( $terms[0], $terms[1] ), wp_list_pluck( $found, 'term_id' ) );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function filter_get_object_terms( $terms ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $term_ids = wp_list_pluck( $terms, 'term_id' );
</span><span class="cx" style="display: block; padding: 0 10px">                // all terms should still be objects
</span></span></pre>
</div>
</div>

</body>
</html>