miguel.nz

Get custom fields from a custom post_type post from an external WordPress DB

January 14, 2015   |   1 minute read.

WOAH!. Ok, but in fact it’s dead simple. My goal here was to get custom fields from a custom post_type from another wordpress site. Ace!.

So this is how I solved it:

$wp_bsd = new wpdb(DBUSER, DBPASS, DBNAME DBHOST);
$wp_bsd->show_errors();

$bsd_user = $wp_bsd->get_results("SELECT p.ID, p.post_title as 'name', pm.meta_value as 'phone', pma.meta_value as 'email'
	FROM wp_posts p 
	INNER JOIN wp_postmeta AS pm  ON pm.post_id  = p.ID
	INNER JOIN wp_postmeta AS pma ON pma.post_id = p.ID
	WHERE
	pma.meta_key = 'email' AND
	pm.meta_key = 'phone' AND
	p.post_type = 'people' AND 
	p.post_status = 'publish'");

DBUSER, DBPASS, DBNAME and DBHOST are just the credentials of the website. This will return an array with all my posts and custom fields. Ta dah!