0

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?

2

Check this wp_set_object_terms().

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.