Get product details from wordpress database and save to other database php,mysql,wordpress

$allpro = mysql_query(" SELECT wp_posts.ID,wp_posts.post_content, wp_posts.post_title, pm1.meta_value AS field1, pm2.meta_value AS field2
FROM wp_posts
LEFT JOIN wp_postmeta AS pm1 ON ( wp_posts.ID = pm1.post_id
AND pm1.meta_key = '_price' )
LEFT JOIN wp_postmeta AS pm2 ON ( wp_posts.ID = pm2.post_id
AND pm2.meta_key = '_thumbnail_id' )
WHERE wp_posts.post_type = 'product'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC

$productall = array();
while($pr = mysql_fetch_array($allpro))

		$im = mysql_fetch_array(mysql_query("select `guid` from `wp_posts` where `ID` = '".$pr['field2']."'"));
		$productall['ID'][] = $pr['ID'];
		$productall['post_title'][] = $pr['post_title'];
		$productall['post_content'][] = $pr['post_content'];
		$productall['price'][] = $pr['field1'];
		$productall['image'][] = $im['guid'];

$totalcount = count($productall['ID']);
//connect to database you want to save products to
for($p=1;$p< $totalcount;$p++)
		$num = mysql_num_rows(mysql_query("select * from `bb_products` where `postid_in_wp` = '".$productall['ID'][$p]."'"));
				mysql_query("insert into `bb_products`(`title`,`description`,`price`,`image`,`postid_in_wp`) values('".$productall['post_title'][$p]."','".$productall['post_content'][$p]."','".$productall['price'][$p]."','".$productall['image'][$p]."','".$productall['ID'][$p]."')");



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s