Well, I have 2 custom post types:
A = Properties B = Property Owners
Inside CPT Properties, I was created a Meta Box to:
- Search a registred Owner (CPT) by VAT number (CPF in Brazil)
- Relate Owner with de Property
I have the CPTs and Meta Boxes. The Meta Boxe "Search Onwer" it is making the ajax request to the server, using this query
function getOwner($post){
global $wpdb;
$cpfcode = $wpdb->escape($post['cpfCode']);
$query = 'SELECT
post_id,
meta_key,
meta_value
FROM
wp_postmeta
WHERE
post_id = (SELECT post_id FROM wp_postmeta WHERE meta_value = "'.$cpfcode.'");';
$keys = $wpdb->get_results($query, OBJECT);
if ($keys) { return $keys; } else { echo "error"; };
exit();
}
and returning an json object with the meta_values and meta_keys like this:
[
{
"post_id" :"112",
"meta_key" :"owner_vat",
"meta_value":"123.456.789-10"
},
{
"post_id" :"112",
"meta_key" :"owner_name",
"meta_value":"Jonh Doe"
},
{
"post_id" :"112",
"meta_key" :"owner_city",
"meta_value":"Rio de Janeiro"
},
]
Now, I need, using jQuery, insert this values in some fields. The problem is do the each or the for, I don't know.