Prevent duplicate posts in wp_insert_post using custom fields

My source links are something linke this :

http://sample.com/entertainment/default.aspx?tabid=2305&conid=102950
http://sample.com/entertainment/default.aspx?tabid=2418&conid=104330
http://sample.com/entertainment/default.aspx?tabid=2429&conid=104264
http://sample.com/entertainment/default.aspx?tabid=2305&conid=102949
.
.
.

I cache content form links.I use wp_insert_post to post cached content from source site to wordpress:

 $my_post = array(
'post_title' => "$title",
'post_content' => "$content",
'post_status' => 'draft',
'post_author' => 1,
'post_category' => array(1),
);
 wp_insert_post( $my_post );

I want to put each link in custom fields and in the next cache , before post to WP , check new links with links in custom fields. If link is repeated , prevent insert content.

Sorry for my bad description.

5 Answers
5

To save the link in the post meta you can use update_post_meta

like this for example:

$url = "http://sample.com/entertainment/default.aspx?tabid=2305&conid=102950"
$my_post = array(
    'post_title' => "$title",
    'post_content' => "$content",
    'post_status' => 'draft',
    'post_author' => 1,
    'post_category' => array(1),
);

$post_id =  wp_insert_post( $my_post );
update_post_meta($post_id,'source_link',$url);

and to prevent the insertion add a simple conditional check:

$args = array("meta_key" => "source_link", "meta_value" =>$url);
$posts = get_posts($args);
if (count($posts) < 0){
    //add new post
}

if (count($posts) < 0){
    //add new post
}

is not working, change it to

if (empty($posts)){ 
    //add new post
}

Leave a Comment