Wordpress how to prevent duplicate post by checking if post title exist before running "wp_insert_post"? Wordpress how to prevent duplicate post by checking if post title exist before running "wp_insert_post"? wordpress wordpress

Wordpress how to prevent duplicate post by checking if post title exist before running "wp_insert_post"?


You can use get_page_by_title() as it supports custom post types now.

if (!get_page_by_title($title, OBJECT, 'skarabeepublication')) :    $my_post = array(        'post_title'=>$title,        'post_content'=>'my contents',        'post_status'=>'draft',        'post_type'=>'skarabeepublication',        'post_author'=>1,    );    wp_insert_post($my_post);endif;

Codex information here


Surprised not to see mention of post_exists function in wp-includes/post.php. See entry on wpseek. There is no entry in the codex. At it's simplest it works like get_page_by_title but returns a post id (or 0 if not found) instead of the object (or null).

$post_id = post_exists( $my_title );if (!$post_id) {    // code here}


Sorry for the late response. I used what Robot says in the comment and this solved my problem. Thanks

$post_if = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_title like '$title_from_soap'");if($post_if < 1){    //code here}