Month: April 2014

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']."'"));
		//print_r($im['guid']);
		$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]."'"));
		if($num)
		{
		
		}
		else
		{
				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]."')");
		}


}