Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

static-blur on "Imported csv file won't update posts using wp_insert_post()"

$
0
0

Hi

Long time lurker here on the forum.
Normally an hour or so searching and I find a solution to my issue, but
this time I've had no luck.

I'm using the code below to upload csv file and import the posts into the custom post type, everything is working except I'm unable to figure out how to get wp_insert_post() to update a published post which shares the same ID.

Instead it just duplicates a new post, I'm using import_id to grab the id from the csv column which works if the id doesn't exist. But updating is where I'm stuck.

Any tips or advice in this area would be much appreciated.

<?php $file = wp_import_handle_upload();

	if(isset($file['file'])) {

		$row = 0;
		if (($handle = fopen($file['file'], "r")) !== FALSE) {
		    while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {

		    	if($row == 0) {
		    		// Don't do anything with the header row
		    		$row++;
		    		continue;
		    	} else {
		    		$row++;

		// Get the post data
	$postinfo = array (
		'import_id'	=> $data[0],
		'post_type'     => '$post_type',
		'post_title'    => $data[2] .' ('. $data[1].')',
		'post_content' 	=> $data[3],
		'post_status'   => 'publish',
			);

 $newpost = wp_insert_post($postinfo);
}?>

Viewing all articles
Browse latest Browse all 8245

Trending Articles