Question: I have this little code to insert posts via custom form:

```
 $post = array(
     'post_author' => 1,
     'post_title' => 'my title,
     'post_content' => 'my content',
     'post_category' => array(3),
     'post_type' => 'post',
  );

if ( $post_id = wp_insert_post( $post ) ){
    echo 'post added!';
 }

```

but now I need to insert the last $post\_id to `term_relationships` table,

basic scheme of term\_relationships:

```
object_id | term_taxnomy_id | term_order
-----------------------------------------

```

Wordpress have a function to update that table?, or I need to build a custom query?

Answer: 
Check this [wp\_set\_object\_terms()](http://codex.wordpress.org/Function_Reference/wp_set_object_terms).
**is\_wp\_error()** functions return the post\_ID on success. The value 0 or WP\_Error on failure.

```
$post = array(
 'post_author' => 1,
 'post_title' => 'my title,
 'post_content' => 'my content',
 'post_category' => array(3),
 'post_type' => 'post',
);

$ins_post = wp_insert_post($post);

if(is_wp_error($ins_post)){
    echo 'error';
}else{
    echo 'post added! <br>';
    echo 'post_id :'.$ins_post ;
}

```