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.
To save the link in the post meta you can use
update_post_meta
like this for example:
and to prevent the insertion add a simple conditional check:
is not working, change it to
That’s pretty strange issue which everyone will face especially if they call this function inside some loop
<foreach>
<for>
<while>
etc.You should try this
}
Notice
get_page_by_title
function which determines whether a post with same title exists or not, if yes it doesn’t callwp_insert_post
.I hope this will help someone struggling with the same problem.
Maybe if you use something like a wpdb query including the wp_posts and wp_postmeta searching this meta before inserting the post…
If the problem, as per one answer above, is that the function is occurring within a loop of some kind, then the classic – and less resource-intensive – way to solve is to introduce a flag.
If the problem isn’t that the function has run inside a loop of some kind, then I’d be interested to know what did cause it: It could be that the function is attached to a hook that runs twice over the course of the entire script: Maybe “update_post” runs twice, for example. In that case, you might resort to one of the other choices offered in the other answers, though they really amount to creating flags by other means (the designated post title is a flag), and the simpler kind of flag variable could be passed along if necessary.
Don’t user custom fields to fix it, you should better try this: