Support

Account

Home Forums General Issues Update_field() don't work correctly with terms

Solved

Update_field() don't work correctly with terms

  • Hello,

    After creating a term i want to create a post. And i try to link the relation-field in Term with this new Post.

    My Code:

    function my_create($term_id, $tt_id, $taxonomy) {
      // gets term_data for Post
      $term = get_term($term_id, 'interpret');
      // Create the Post and Save ID to $post_id
      $post = array(
          'post_title' => $term->name,
          'post_content' => $term->slug,
      );
      $post_id = wp_insert_post($post);
      // if Post was created
      if ($post_id) {
        // get Post by ID
        $link_to = get_post($post_id);
        // Post->ID
        $link = $link_to->ID;
        update_field('field_558e8544d5660', $link, $term_id);
      }
    }

    add_action(‘create_interpret’, ‘my_create’, 10, 3);

    The Post is right, the term also, but the Relation-Filed in the term is empty.

    In table “wp_options” are two new lines:
    9614 _interpret_49_link_to_album_post_type field_558e8544d5660
    9613 interpret_49_link_to_album_post_type // empty

    In table “wp_postmeta” are thre new lines
    863 208 _edit_lock 1435413264:1
    862 49 _link_to_album_post_type field_558e8544d5660
    861 49 link_to_album_post_type 208 // The ID of the new POST

    The Custom-Field is only for this custom taxonomy, not for the normal posts.

    Can anyone show me the error in the code?

    Greetings
    Kati

  • You are setting a custom field on a term. For this you need to include the taxonomy as part of the $post_id. I have altered the update_field() call of your code

    
    function my_create($term_id, $tt_id, $taxonomy) {
      // gets term_data for Post
      $term = get_term($term_id, 'interpret');
      // Create the Post and Save ID to $post_id
      $post = array(
          'post_title' => $term->name,
          'post_content' => $term->slug,
      );
      $post_id = wp_insert_post($post);
      // if Post was created
      if ($post_id) {
        // get Post by ID
        $link_to = get_post($post_id);
        // Post->ID
        $link = $link_to->ID;
        update_field('field_558e8544d5660', $link, 'interpret_'.$term_id);
      }
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Update_field() don't work correctly with terms’ is closed to new replies.