wp_set_object_terms creates unnecessary links?
-
Hello,
I am trying to automatically add terms of my taxonomy ‘caps’ to the post whenever post is updated. So I created a function and added it as action. However, when I look at the taxonomies – count of post for each term is incremented every time I save post. Interestingly that this does not happens with terms in UTF-8.
What I am doing wrong?
add_action( 'save_post', 'save_my_post' );
function save_my_post($id)
{
$post = get_post($id);
if(!isset($post)) return;
if ( 'post' == $post->post_type )
clean_post_cache($post->ID);
$the_caps = get_caps($post);
// need to merge existing caps with the new
$old_caps = wp_get_post_terms($post->ID, 'caps', array('fields'=>'names'));
$new_caps = array_diff($the_caps,$old_caps);
wp_set_object_terms( $post->ID, $new_caps, 'caps',true);
if ( 'post' == $post->post_type )
clean_post_cache($post->ID);
return true;
}
- The topic ‘wp_set_object_terms creates unnecessary links?’ is closed to new replies.