/** * Retrieves the terms associated with the given object(s), in the supplied taxonomies. * * @since 2.3.0 * @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`. * Introduced `$parent` argument. * @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments. When `$fields` is 'all' or * 'all_with_object_id', an array of `WP_Term` objects will be returned. * * @global wpdb $wpdb WordPress database abstraction object. * * @param int|array $object_ids The ID(s) of the object(s) to retrieve. * @param string|array $taxonomies The taxonomies to retrieve terms from. * @param array|string $args { * Array of arguments. * @type string $orderby Field by which results should be sorted. Accepts 'name', 'count', 'slug', * 'term_group', 'term_order', 'taxonomy', 'parent', or 'term_taxonomy_id'. * Default 'name'. * @type string $order Sort order. Accepts 'ASC' or 'DESC'. Default 'ASC'. * @type string $fields Fields to return for matched terms. Accepts 'all', 'ids', 'names', and * 'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result * in an array of term objects being returned, 'ids' will return an array of * integers, and 'names' an array of strings. * @type int $parent Optional. Limit results to the direct children of a given term ID. * @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. * } * @return array|WP_Error The requested term data or empty array if no terms found. * WP_Error if any of the $taxonomies don't exist. */ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { global $wpdb; if (empty($object_ids) || empty($taxonomies)) { return array(); } if (!is_array($taxonomies)) { $taxonomies = array($taxonomies); } foreach ($taxonomies as $taxonomy) { if (!taxonomy_exists($taxonomy)) { return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); } } if (!is_array($object_ids)) { $object_ids = array($object_ids); } $object_ids = array_map('intval', $object_ids); $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all', 'parent' => '', 'update_term_meta_cache' => true, 'meta_query' => ''); $args = wp_parse_args($args, $defaults); $terms = array(); if (count($taxonomies) > 1) { foreach ($taxonomies as $index => $taxonomy) { $t = get_taxonomy($taxonomy); if (isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args)) { unset($taxonomies[$index]); $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args))); } } } else { $t = get_taxonomy($taxonomies[0]); if (isset($t->args) && is_array($t->args)) { $args = array_merge($args, $t->args); } } $orderby = $args['orderby']; $order = $args['order']; $fields = $args['fields']; if (in_array($orderby, array('term_id', 'name', 'slug', 'term_group'))) { $orderby = "t.{$orderby}"; } elseif (in_array($orderby, array('count', 'parent', 'taxonomy', 'term_taxonomy_id'))) { $orderby = "tt.{$orderby}"; } elseif ('term_order' === $orderby) { $orderby = 'tr.term_order'; } elseif ('none' === $orderby) { $orderby = ''; $order = ''; } else { $orderby = 't.term_id'; } // tt_ids queries can only be none or tr.term_taxonomy_id if ('tt_ids' == $fields && !empty($orderby)) { $orderby = 'tr.term_taxonomy_id'; } if (!empty($orderby)) { $orderby = "ORDER BY {$orderby}"; } $order = strtoupper($order); if ('' !== $order && !in_array($order, array('ASC', 'DESC'))) { $order = 'ASC'; } $taxonomy_array = $taxonomies; $object_id_array = $object_ids; $taxonomies = "'" . implode("', '", $taxonomies) . "'"; $object_ids = implode(', ', $object_ids); $select_this = ''; if ('all' == $fields) { $select_this = 't.*, tt.*'; } elseif ('ids' == $fields) { $select_this = 't.term_id'; } elseif ('names' == $fields) { $select_this = 't.name'; } elseif ('slugs' == $fields) { $select_this = 't.slug'; } elseif ('all_with_object_id' == $fields) { $select_this = 't.*, tt.*, tr.object_id'; } $where = array("tt.taxonomy IN ({$taxonomies})", "tr.object_id IN ({$object_ids})"); if ('' !== $args['parent']) { $where[] = $wpdb->prepare('tt.parent = %d', $args['parent']); } // 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']); } $where = implode(' AND ', $where); $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}"; $objects = false; if ('all' == $fields || 'all_with_object_id' == $fields) { $_terms = $wpdb->get_results($query); $object_id_index = array(); foreach ($_terms as $key => $term) { $term = sanitize_term($term, $taxonomy, 'raw'); $_terms[$key] = $term; if (isset($term->object_id)) { $object_id_index[$key] = $term->object_id; } } update_term_cache($_terms); $_terms = array_map('get_term', $_terms); // Re-add the object_id data, which is lost when fetching terms from cache. if ('all_with_object_id' === $fields) { foreach ($_terms as $key => $_term) { if (isset($object_id_index[$key])) { $_term->object_id = $object_id_index[$key]; } } } $terms = array_merge($terms, $_terms); $objects = true; } elseif ('ids' == $fields || 'names' == $fields || 'slugs' == $fields) { $_terms = $wpdb->get_col($query); $_field = 'ids' == $fields ? 'term_id' : 'name'; foreach ($_terms as $key => $term) { $_terms[$key] = sanitize_term_field($_field, $term, $term, $taxonomy, 'raw'); } $terms = array_merge($terms, $_terms); } elseif ('tt_ids' == $fields) { $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM {$wpdb->term_relationships} AS tr INNER JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ({$object_ids}) AND tt.taxonomy IN ({$taxonomies}) {$orderby} {$order}"); foreach ($terms as $key => $tt_id) { $terms[$key] = sanitize_term_field('term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw'); // 0 should be the term id, however is not needed when using raw context. } } // 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); } if (!$terms) { $terms = array(); } elseif ($objects && 'all_with_object_id' !== $fields) { $_tt_ids = array(); $_terms = array(); foreach ($terms as $term) { if (in_array($term->term_taxonomy_id, $_tt_ids)) { continue; } $_tt_ids[] = $term->term_taxonomy_id; $_terms[] = $term; } $terms = $_terms; } elseif (!$objects) { $terms = array_values(array_unique($terms)); } /** * Filter the terms for a given object or objects. * * @since 4.2.0 * * @param array $terms An array of terms for the given object or objects. * @param array $object_id_array Array of object IDs for which `$terms` were retrieved. * @param array $taxonomy_array Array of taxonomies from which `$terms` were retrieved. * @param array $args An array of arguments for retrieving terms for the given * object(s). See wp_get_object_terms() for details. */ $terms = apply_filters('get_object_terms', $terms, $object_id_array, $taxonomy_array, $args); /** * Filter the terms for a given object or objects. * * The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The * {@see 'get_object_terms'} filter is recommended as an alternative. * * @since 2.8.0 * * @param array $terms An array of terms for the given object or objects. * @param int|array $object_ids Object ID or array of IDs. * @param string $taxonomies SQL-formatted (comma-separated and quoted) list of taxonomy names. * @param array $args An array of arguments for retrieving terms for the given object(s). * See {@see wp_get_object_terms()} for details. */ return apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args); }
/** * Lazy-loads termmeta for located posts. * 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` instance, 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 method * detects the fact, and then primes the metadata cache for all terms attached to all posts in the loop, * with a single database query. * This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it * directly, from either inside or outside the `WP_Query` object. * @param mixed $check The `$check` param passed from the 'get_term_metadata' hook. * @param int $term_id ID of the term whose metadata is being cached. * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be * another value if filtered by a plugin. */ public function lazyload_term_meta($check, $term_id) { /* * We only do this once per `WP_Query` instance. * Can't use `remove_filter()` because of non-unique object hashes. */ if ($this->updated_term_meta_cache) { return $check; } // We can only lazyload if the entire post object is present. $posts = array(); foreach ($this->posts as $post) { if ($post instanceof WP_Post) { $posts[] = $post; } } if (!empty($posts)) { // 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) { // 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; } } } } } /* * Only update the metadata cache for terms belonging to these posts if the term_id passed * to `get_term_meta()` matches one of those terms. This prevents a single call to * `get_term_meta()` from priming metadata for all `WP_Query` objects. */ if (isset($term_ids[$term_id])) { update_termmeta_cache(array_keys($term_ids)); $this->updated_term_meta_cache = true; } } // If no terms were found, there's no need to run this again. if (empty($term_ids)) { $this->updated_term_meta_cache = true; } return $check; }
function get_term_custom($term_id) { $term_id = (int) $term_id; if (!wp_cache_get($term_id, 'term_meta')) { update_termmeta_cache($term_id); } return wp_cache_get($term_id, 'term_meta'); }
/** * Adds any terms from the given IDs to the cache that do not already exist in cache. * * @since 4.6.0 * @access private * * @global wpdb $wpdb WordPress database abstraction object. * * @param array $term_ids Array of term IDs. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. */ function _prime_term_caches($term_ids, $update_meta_cache = true) { global $wpdb; $non_cached_ids = _get_non_cached_ids($term_ids, 'terms'); if (!empty($non_cached_ids)) { $fresh_terms = $wpdb->get_results(sprintf("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", join(",", array_map('intval', $non_cached_ids)))); update_term_cache($fresh_terms, $update_meta_cache); if ($update_meta_cache) { update_termmeta_cache($non_cached_ids); } } }
/** * Get terms, based on query_vars. * * @param 4.6.0 * @access public * * @global wpdb $wpdb WordPress database abstraction object. * * @return array */ public function get_terms() { global $wpdb; $this->parse_query($this->query_vars); $args = $this->query_vars; // Set up meta_query so it's available to 'pre_get_terms'. $this->meta_query = new WP_Meta_Query(); $this->meta_query->parse_query_vars($args); /** * Fires before terms are retrieved. * * @since 4.6.0 * * @param WP_Term_Query $this Current instance of WP_Term_Query. */ do_action('pre_get_terms', $this); $taxonomies = $args['taxonomy']; // Save queries by not crawling the tree in the case of multiple taxes or a flat tax. $has_hierarchical_tax = false; if ($taxonomies) { foreach ($taxonomies as $_tax) { if (is_taxonomy_hierarchical($_tax)) { $has_hierarchical_tax = true; } } } if (!$has_hierarchical_tax) { $args['hierarchical'] = false; $args['pad_counts'] = false; } // 'parent' overrides 'child_of'. if (0 < intval($args['parent'])) { $args['child_of'] = false; } if ('all' == $args['get']) { $args['childless'] = false; $args['child_of'] = 0; $args['hide_empty'] = 0; $args['hierarchical'] = false; $args['pad_counts'] = false; } /** * Filters the terms query arguments. * * @since 3.1.0 * * @param array $args An array of get_terms() arguments. * @param array $taxonomies An array of taxonomies. */ $args = apply_filters('get_terms_args', $args, $taxonomies); // Avoid the query if the queried parent/child_of term has no descendants. $child_of = $args['child_of']; $parent = $args['parent']; if ($child_of) { $_parent = $child_of; } elseif ($parent) { $_parent = $parent; } else { $_parent = false; } if ($_parent) { $in_hierarchy = false; foreach ($taxonomies as $_tax) { $hierarchy = _get_term_hierarchy($_tax); if (isset($hierarchy[$_parent])) { $in_hierarchy = true; } } if (!$in_hierarchy) { return array(); } } $orderby = $this->parse_orderby($this->query_vars['orderby']); $order = $this->parse_order($this->query_vars['order']); if ($taxonomies) { $this->sql_clauses['where']['taxonomy'] = "tt.taxonomy IN ('" . implode("', '", array_map('esc_sql', $taxonomies)) . "')"; } $exclude = $args['exclude']; $exclude_tree = $args['exclude_tree']; $include = $args['include']; $inclusions = ''; if (!empty($include)) { $exclude = ''; $exclude_tree = ''; $inclusions = implode(',', wp_parse_id_list($include)); } if (!empty($inclusions)) { $this->sql_clauses['where']['inclusions'] = 't.term_id IN ( ' . $inclusions . ' )'; } $exclusions = array(); if (!empty($exclude_tree)) { $exclude_tree = wp_parse_id_list($exclude_tree); $excluded_children = $exclude_tree; foreach ($exclude_tree as $extrunk) { $excluded_children = array_merge($excluded_children, (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids', 'hide_empty' => 0))); } $exclusions = array_merge($excluded_children, $exclusions); } if (!empty($exclude)) { $exclusions = array_merge(wp_parse_id_list($exclude), $exclusions); } // 'childless' terms are those without an entry in the flattened term hierarchy. $childless = (bool) $args['childless']; if ($childless) { foreach ($taxonomies as $_tax) { $term_hierarchy = _get_term_hierarchy($_tax); $exclusions = array_merge(array_keys($term_hierarchy), $exclusions); } } if (!empty($exclusions)) { $exclusions = 't.term_id NOT IN (' . implode(',', array_map('intval', $exclusions)) . ')'; } else { $exclusions = ''; } /** * Filters the terms to exclude from the terms query. * * @since 2.3.0 * * @param string $exclusions `NOT IN` clause of the terms query. * @param array $args An array of terms query arguments. * @param array $taxonomies An array of taxonomies. */ $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args, $taxonomies); if (!empty($exclusions)) { // Must do string manipulation here for backward compatibility with filter. $this->sql_clauses['where']['exclusions'] = preg_replace('/^\\s*AND\\s*/', '', $exclusions); } if (!empty($args['name'])) { $names = (array) $args['name']; foreach ($names as &$_name) { // `sanitize_term_field()` returns slashed data. $_name = stripslashes(sanitize_term_field('name', $_name, 0, reset($taxonomies), 'db')); } $this->sql_clauses['where']['name'] = "t.name IN ('" . implode("', '", array_map('esc_sql', $names)) . "')"; } if (!empty($args['slug'])) { if (is_array($args['slug'])) { $slug = array_map('sanitize_title', $args['slug']); $this->sql_clauses['where']['slug'] = "t.slug IN ('" . implode("', '", $slug) . "')"; } else { $slug = sanitize_title($args['slug']); $this->sql_clauses['where']['slug'] = "t.slug = '{$slug}'"; } } if (!empty($args['term_taxonomy_id'])) { if (is_array($args['term_taxonomy_id'])) { $tt_ids = implode(',', array_map('intval', $args['term_taxonomy_id'])); $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})"; } else { $this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare("tt.term_taxonomy_id = %d", $args['term_taxonomy_id']); } } if (!empty($args['name__like'])) { $this->sql_clauses['where']['name__like'] = $wpdb->prepare("t.name LIKE %s", '%' . $wpdb->esc_like($args['name__like']) . '%'); } if (!empty($args['description__like'])) { $this->sql_clauses['where']['description__like'] = $wpdb->prepare("tt.description LIKE %s", '%' . $wpdb->esc_like($args['description__like']) . '%'); } if ('' !== $parent) { $parent = (int) $parent; $this->sql_clauses['where']['parent'] = "tt.parent = '{$parent}'"; } $hierarchical = $args['hierarchical']; if ('count' == $args['fields']) { $hierarchical = false; } if ($args['hide_empty'] && !$hierarchical) { $this->sql_clauses['where']['count'] = 'tt.count > 0'; } $number = $args['number']; $offset = $args['offset']; // Don't limit the query results when we have to descend the family tree. if ($number && !$hierarchical && !$child_of && '' === $parent) { if ($offset) { $limits = 'LIMIT ' . $offset . ',' . $number; } else { $limits = 'LIMIT ' . $number; } } else { $limits = ''; } if (!empty($args['search'])) { $this->sql_clauses['where']['search'] = $this->get_search_sql($args['search']); } // Meta query support. $join = ''; $distinct = ''; // Reparse meta_query query_vars, in case they were modified in a 'pre_get_terms' callback. $this->meta_query->parse_query_vars($this->query_vars); $mq_sql = $this->meta_query->get_sql('term', 't', 'term_id'); $meta_clauses = $this->meta_query->get_clauses(); if (!empty($meta_clauses)) { $join .= $mq_sql['join']; $this->sql_clauses['where']['meta_query'] = preg_replace('/^\\s*AND\\s*/', '', $mq_sql['where']); $distinct .= "DISTINCT"; } $selects = array(); switch ($args['fields']) { case 'all': $selects = array('t.*', 'tt.*'); break; case 'ids': case 'id=>parent': $selects = array('t.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy'); break; case 'names': $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy'); break; case 'count': $orderby = ''; $order = ''; $selects = array('COUNT(*)'); break; case 'id=>name': $selects = array('t.term_id', 't.name', 'tt.count', 'tt.taxonomy'); break; case 'id=>slug': $selects = array('t.term_id', 't.slug', 'tt.count', 'tt.taxonomy'); break; } $_fields = $args['fields']; /** * Filters the fields to select in the terms query. * * Field lists modified using this filter will only modify the term fields returned * by the function when the `$fields` parameter set to 'count' or 'all'. In all other * cases, the term fields in the results array will be determined by the `$fields` * parameter alone. * * Use of this filter can result in unpredictable behavior, and is not recommended. * * @since 2.8.0 * * @param array $selects An array of fields to select for the terms query. * @param array $args An array of term query arguments. * @param array $taxonomies An array of taxonomies. */ $fields = implode(', ', apply_filters('get_terms_fields', $selects, $args, $taxonomies)); $join .= " INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id"; $where = implode(' AND ', $this->sql_clauses['where']); $pieces = array('fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits'); /** * Filters the terms query SQL clauses. * * @since 3.1.0 * * @param array $pieces Terms query SQL clauses. * @param array $taxonomies An array of taxonomies. * @param array $args An array of terms query arguments. */ $clauses = apply_filters('terms_clauses', compact($pieces), $taxonomies, $args); $fields = isset($clauses['fields']) ? $clauses['fields'] : ''; $join = isset($clauses['join']) ? $clauses['join'] : ''; $where = isset($clauses['where']) ? $clauses['where'] : ''; $distinct = isset($clauses['distinct']) ? $clauses['distinct'] : ''; $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : ''; $order = isset($clauses['order']) ? $clauses['order'] : ''; $limits = isset($clauses['limits']) ? $clauses['limits'] : ''; if ($where) { $where = "WHERE {$where}"; } $this->sql_clauses['select'] = "SELECT {$distinct} {$fields}"; $this->sql_clauses['from'] = "FROM {$wpdb->terms} AS t {$join}"; $this->sql_clauses['orderby'] = $orderby ? "ORDER BY {$orderby} {$order}" : ''; $this->sql_clauses['limits'] = $limits; $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; // $args can be anything. Only use the args defined in defaults to compute the key. $key = md5(serialize(wp_array_slice_assoc($args, array_keys($this->query_var_defaults))) . serialize($taxonomies) . $this->request); $last_changed = wp_cache_get('last_changed', 'terms'); if (!$last_changed) { $last_changed = microtime(); wp_cache_set('last_changed', $last_changed, 'terms'); } $cache_key = "get_terms:{$key}:{$last_changed}"; $cache = wp_cache_get($cache_key, 'terms'); if (false !== $cache) { if ('all' === $_fields) { $cache = array_map('get_term', $cache); } return $cache; } if ('count' == $_fields) { return $wpdb->get_var($this->request); } $terms = $wpdb->get_results($this->request); if ('all' == $_fields) { update_term_cache($terms); } // Prime termmeta cache. if ($args['update_term_meta_cache']) { $term_ids = wp_list_pluck($terms, 'term_id'); update_termmeta_cache($term_ids); } if (empty($terms)) { wp_cache_add($cache_key, array(), 'terms', DAY_IN_SECONDS); return array(); } if ($child_of) { foreach ($taxonomies as $_tax) { $children = _get_term_hierarchy($_tax); if (!empty($children)) { $terms = _get_term_children($child_of, $terms, $_tax); } } } // Update term counts to include children. if ($args['pad_counts'] && 'all' == $_fields) { foreach ($taxonomies as $_tax) { _pad_term_counts($terms, $_tax); } } // Make sure we show empty categories that have children. if ($hierarchical && $args['hide_empty'] && is_array($terms)) { foreach ($terms as $k => $term) { if (!$term->count) { $children = get_term_children($term->term_id, $term->taxonomy); if (is_array($children)) { foreach ($children as $child_id) { $child = get_term($child_id, $term->taxonomy); if ($child->count) { continue 2; } } } // It really is empty. unset($terms[$k]); } } } $_terms = array(); if ('id=>parent' == $_fields) { foreach ($terms as $term) { $_terms[$term->term_id] = $term->parent; } } elseif ('ids' == $_fields) { foreach ($terms as $term) { $_terms[] = $term->term_id; } } elseif ('names' == $_fields) { foreach ($terms as $term) { $_terms[] = $term->name; } } elseif ('id=>name' == $_fields) { foreach ($terms as $term) { $_terms[$term->term_id] = $term->name; } } elseif ('id=>slug' == $_fields) { foreach ($terms as $term) { $_terms[$term->term_id] = $term->slug; } } if (!empty($_terms)) { $terms = $_terms; } // Hierarchical queries are not limited, so 'offset' and 'number' must be handled now. if ($hierarchical && $number && is_array($terms)) { if ($offset >= count($terms)) { $terms = array(); } else { $terms = array_slice($terms, $offset, $number, true); } } wp_cache_add($cache_key, $terms, 'terms', DAY_IN_SECONDS); if ('all' === $_fields) { $terms = array_map('get_term', $terms); } $this->terms = $terms; return $this->terms; }
/** * Lazyloads term meta for queued terms. * * This method is public so that it can be used as a filter callback. As a rule, there * is no need to invoke it directly. * * @since 4.5.0 * @access public * * @param mixed $check The `$check` param passed from the 'get_term_metadata' hook. * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be * another value if filtered by a plugin. */ public function lazyload_term_meta($check) { if (!empty($this->pending_objects['term'])) { update_termmeta_cache(array_keys($this->pending_objects['term'])); // No need to run again for this set of terms. $this->reset_queue('term'); } return $check; }
function save_taxonomy_custom_meta($term_id) { if (isset($_POST['term_icon_value'])) { $value = $_POST['term_icon_value']; $current_value = get_term_meta($term_id, 'pix_term_icon', true); if (empty($current_value)) { $updated = update_term_meta($term_id, 'pix_term_icon', $value); } else { $updated = update_term_meta($term_id, 'pix_term_icon', $value, $current_value); } update_termmeta_cache(array($term_id)); } if (isset($_POST['term_image_value'])) { $value_image = $_POST['term_image_value']; $current_value_image = get_term_meta($term_id, 'pix_term_image', true); if (empty($current_value_image)) { $updated = update_term_meta($term_id, 'pix_term_image', $value_image); } else { $updated = update_term_meta($term_id, 'pix_term_image', $value_image, $current_value_image); } update_termmeta_cache(array($term_id)); } }
/** * Retrieve term custom fields * * * @package Simple Taxonomy Meta * * @uses $id * @uses $wpdb * * @param int $term_taxonomy_id term ID * @return array {@internal Missing Description}} */ function get_term_taxonomy_custom($term_taxonomy_id = 0) { global $id; if (!$term_taxonomy_id) { $term_taxonomy_id = (int) $id; } $term_taxonomy_id = (int) $term_taxonomy_id; if (!wp_cache_get($term_taxonomy_id, 'term_taxo_meta')) { update_termmeta_cache($term_taxonomy_id); } return wp_cache_get($term_taxonomy_id, 'term_taxo_meta'); }