本文整理汇总了PHP中update_termmeta_cache函数的典型用法代码示例。如果您正苦于以下问题:PHP update_termmeta_cache函数的具体用法?PHP update_termmeta_cache怎么用?PHP update_termmeta_cache使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了update_termmeta_cache函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: wp_get_object_terms
//.........这里部分代码省略.........
$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);
}
示例2: lazyload_term_meta
/**
* 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;
}
示例3: get_term_custom
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');
}
示例4: _prime_term_caches
/**
* 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);
}
}
}
示例5: get_terms
//.........这里部分代码省略.........
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;
}
}
}
示例6: lazyload_term_meta
/**
* 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;
}
示例7: save_taxonomy_custom_meta
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));
}
}
示例8: get_term_taxonomy_custom
/**
* 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');
}