• 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;
    }

Viewing 1 replies (of 1 total)
  • Thread Starter hutorny

    (@hutorny)

    It took me 24 hours to find what is the problem
    save_post action is called on inserting new post and new revision.
    After adding this check in the beginning:

    if(wp_is_post_revision($id))
    return true;

    the problems seems to be gone

Viewing 1 replies (of 1 total)
  • The topic ‘wp_set_object_terms creates unnecessary links?’ is closed to new replies.