2

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.

3

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
}
  • Thanks a lot for this wonderful solution !! But shouldn't it be (count($posts) == 0) instead of "<" – user44343 May 25 '14 at 20:21
9

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

if (!get_page_by_title($title, 'OBJECT', 'post') ){

 $my_post = array('post_title' => $title,
                         'post_content' => 'Content',
                         'tags_input' => $tags,
                         'post_category' => array(2),
                         'post_status' => 'publish'
                        );

  $result = wp_insert_post( $my_post );

}

Notice get_page_by_title function which determines whether a post with same title exists or not, if yes it doesn't call wp_insert_post.

I hope this will help someone struggling with the same problem.

  • That's a smart solution. I like its simplicity. Is there get_post_by_title function instead of get_page_by_title and what should the OBJECT to be? – KeepMove Nov 1 '13 at 1:07
2

Maybe if you use something like a wpdb query including the wp_posts and wp_postmeta searching this meta before inserting the post...

$string = 'the_url.html';
$output = $wpdb->get_var($wpdb->prepare("SELECT count(id)
            FROM $wpdb->posts wpo, $wpdb->postmeta wpm
            WHERE wpo.ID = wpm.post_id
            AND wpm.meta_key = 'name_of_ur_meta'
            AND wpm.meta_value = '$string'"));      

if(empty($output)) {
    /* Insert your post */
    } else {
            /* Update the post or do other thing */
    };
1

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.

$ran_already_flag = false;

{loop begins}

if ( ! $ran_already_flag ) {

    $my_post = array('post_title' => $title,
                     'post_content' => 'Content',
                     'tags_input' => $tags,
                     'post_category' => array(2),
                     'post_status' => 'publish'
                    );


    $result = wp_insert_post( $my_post );
    $ran_already_flag = true;

}

{loop ends}

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.

0

Don't user custom fields to fix it, you should better try this:

if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ){
    $post_id = wp_insert_post( $my_post );
}
  • Please add an explanation to your answer: why does this solve the problem? – Sven Aug 24 '14 at 15:27

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.