wordpress wp_insert_post & wp_update_post

What if you use the empty check for the array

Please be aware that it gets the first post/page item in the database even if the post is trashed.

$check_title=get_page_by_title($post_title, 'OBJECT', 'post');

//also var_dump($check_title) for testing only

if (empty($check_title) ){
$my_post = array(
   'post_title'    => $post_title,
  'post_content'  => $post_content,
  'post_status'   => 'publish',
  'post_author'   => $post_author,
  'post_category' => $post_categories
);

wp_insert_post( $my_post );

}

else {


$my_post = array(

   'ID' =>  $check_title->ID,
   'post_title'    => $post_title,
  'post_content'  => $post_content,
  'post_status'   => 'publish',
  'post_author'   => $post_author,
  'post_category' => $post_categories
);

wp_update_post( $my_post );

}

Tags:

Wordpress