Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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
}
share|improve this answer
    
Thanks , its my way! –  Nulled Jun 11 '11 at 12:33
    
Thanks a lot for this wonderful solution !! But shouldn't it be (count($posts) == 0) instead of "<" –  coding May 25 at 20:21

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.

share|improve this answer
    
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? –  Ari Susanto Nov 1 '13 at 1:07

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 */
    };
share|improve this answer
    
Thanks for your solution. –  Nulled Jun 11 '11 at 12:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.