Cập nhật Categories, Tags cho post, wp_set_object_terms, wp_set_post_terms (ok)

Không có sự khác biệt lớn giữa chúng, thực sự wp_set_post_terms()sử dụng wp_set_object_terms()nhưng không kiểm tra thêm cho bạn. Điều đó cũng được ghi nhận trên wp_set_object_terms()trang Codex:

Có lẽ wp_set_post_terms () là một hàm hữu ích hơn, vì nó kiểm tra các giá trị, chuyển đổi các phân loại được phân tách bằng dấu phẩy và xác thực các thuật ngữ phân cấp trong các số nguyên.

<?php
// An array of IDs of categories we want this post to have.
$cat_ids = array(2, 3);
$tag_ids = array(4, 5);
/*
 * If this was coming from the database or another source, we would need to make sure
 * these were integers:

$cat_ids = array_map( 'intval', $cat_ids );
$cat_ids = array_unique( $cat_ids );

 */
$term_taxonomy_ids = wp_set_object_terms(1, $cat_ids, 'post_tag');

if (is_wp_error($term_taxonomy_ids)) {
	echo 'error';
} else {
	echo "success";
}

Thành quả :)

Last updated