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.

Jeff Monteiro
  • 721
  • 1
  • 7
  • 17

2 Answers2

0

I'm not 100% what you are asking for but here is the code for navigating through a JSON request. The code below assumes you have input fields with the same name as your meta_fields.

<input id="owner_vat" type="text"> 
<input id="owner_name" type="text"> 
<input id="owner_city" type="text"> 

success: function(response){
    $.each(response, function(index, value) {
        $('#'+value['meta_key']).val(value['meta_value']);
    });     
}

Hope that helps

cody collicott
  • 261
  • 2
  • 6
  • Cody Collicott, thank you for your help. But your solution not works for me. Anyway, a friend did help me to find the solution. I will post here. – Jeff Monteiro Oct 22 '15 at 20:16
0

Solved with collaboration of Thiago Lewin:

function ajx_cpf(cpfCode){

    var cpf_data = {
                    'action': 'Load_owner',
                    'cpfCode': cpfCode
                    };

    jQuery.ajax({
                type: 'POST',
                url: '<?php echo get_settings('home')?>/wp-admin/admin-ajax.php',
                data: cpf_data,

                success: function(data, textStatus, XMLHttpRequest){
                    loadOwner_sel(data);
                },

                error: function(MLHttpRequest, textStatus, errorThrown){
                    alert(errorThrown);
                }
    });
}

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

        jQuery.each(array, function(index, item) {
            obj[item.meta_key] = item.meta_value;
        });

    document.getElementById('owner_vat').value = post_id;
    document.getElementById('owner_name').value = obj.owner_name;
    document.getElementById('owner_city').value = post_id;
}
Jeff Monteiro
  • 721
  • 1
  • 7
  • 17