返回介绍

add_term_meta()

发布于 2017-09-10 21:23:01 字数 2909 浏览 1666 评论 0 收藏 0

add_term_meta( int $term_id,  string $meta_key,  mixed $meta_value,  bool $unique = false )

Adds metadata to a term.


description


参数

$term_id

(int) (Required) Term ID.

$meta_key

(string) (Required) Metadata name.

$meta_value

(mixed) (Required) Metadata value.

$unique

(bool) (Optional) Whether to bail if an entry with the same key is found for the term.

Default value: false


返回值

(int|WP_Error|bool) Meta ID on success. WP_Error when term_id is ambiguous between taxonomies. False on failure.


源代码

File: wp-includes/taxonomy.php

function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
	// Bail if term meta table is not installed.
	if ( get_option( 'db_version' ) < 34370 ) {
		return false;
	}

	if ( wp_term_is_shared( $term_id ) ) {
		return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id );
	}

	$added = add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );

	// Bust term query cache.
	if ( $added ) {
		wp_cache_set( 'last_changed', microtime(), 'terms' );
	}

	return $added;
}

更新日志

Versiondescription
4.4.0Introduced.

相关函数

Uses

  • wp-includes/taxonomy.php: wp_term_is_shared()
  • wp-includes/cache.php: wp_cache_set()
  • wp-includes/l10n.php: __()
  • wp-includes/option.php: get_option()
  • wp-includes/meta.php: add_metadata()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 1 more use Hide more uses

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 5You must log in to vote on the helpfulness of this note Contributed by Abiral Neupane

    Usage

    add_term_meta( $term_id, $meta_key, $meta_value, $unique );

    Example
    Adds a new custom field with a key name ‘my_term_key’ and value as ‘new_term’

    add_term_meta( 12, "my_term_key" , 'new_term' );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文