0

Well, I have 2 custom post types:

A = Properties B = Property Owners

Inside CPT Properties, I was created a Meta Box to:

  1. Search a registred Owner (CPT) by VAT number (CPF in Brazil)
  2. 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.

1 Answer 1

0

Solved:

function loadOwner_sel(dta,selval) {
   var
        array = JSON.parse(dta),
        obj = {};

        jQuery.each(array, function(index, item) {
            obj[item.meta_key] = item.meta_value;
        });
        document.getElementById('owner_name').value = obj.owner_name;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.