Support

Account

Home Forums General Issues Update Taxonomy of posts selected by acf relation field Reply To: Update Taxonomy of posts selected by acf relation field

  • There is an error in my code I gave you

    This line should be causing an error in php

    
    $term_id = intval(str_replace('term_', $post_id));
    

    it should be

    
    $term_id = intval(str_replace('term_', '', $post_id));
    

    I also set up a test using posts and categories, this is working as expected

    
    add_filter('acf/save_post', 'category_list_save', 20, 1);
    function category_list_save($post_id) {
      $posts = get_field('posts', $post_id);
      $term_id = intval(str_replace('term_', '', $post_id));
      if ($posts) {
        foreach ($posts as $post) {
          // make sure "list" is the taxonomy name and not the term name
          wp_set_post_terms($post->ID, $term_id, 'category', true);
        }
      }
    }
    

    It it’s still not working for you make sure that your taxonomy and post type are attached. https://codex.wordpress.org/Function_Reference/register_taxonomy_for_object_type