I use to following lines of code to get the posts as an array and then send it back to the js which was asking for it by transfer the posts with json_encode to a JSON
function get_feedback() {
if(isset($_POST['postId'])){
$postId = $_POST['postId'];
}
$cToken = get_post_meta($postId, 'cToken', true);
$posts = get_posts(
array(
'numberposts' => -1,
'post_type' => 'post',
'category' => 'Feedback',
'meta_key' => 'cToken',
'meta_value' => $cToken
)
);
echo json_encode($posts);
wp_die();
}
I am then trying to post a list of all post id's to a div on my WordPressPage with following code
function getFeedback(postId){
$(".show_company").hide();
$(".show_feedback").show();
$.ajax({
type: "POST",
url: ajax_object.ajax_url,
data:{
action:'get_feedback',
postId: postId,
},
success:function(response) {
$("#result").html(response);
alert(response[0]);
}
});
}
But it is only messaging undefined.
the object passed from php to js looks like this if i show it plain:
[{"ID":387,"post_author":"9","post_date":"2018-05-17 23:06:35","post_date_gmt":"2018-05-17 23:06:35","post_content":"","post_title":"jitz - grad - 0106","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"jitz-grad-0106","to_ping":"","pinged":"","post_modified":"2018-05-17 23:06:35","post_modified_gmt":"2018-05-17 23:06:35","post_content_filtered":"","post_parent":0,"guid":"http:\/\/localhost\/wp\/jitz-grad-0106\/","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"0","filter":"raw"},{"ID":386,"post_author":"9","post_date":"2018-05-17 23:06:16","post_date_gmt":"2018-05-17 23:06:16","post_content":"","post_title":"hergott - 0001 - 4","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"hergott-0001-4","to_ping":"","pinged":"","post_modified":"2018-05-17 23:06:16","post_modified_gmt":"2018-05-17 23:06:16","post_content_filtered":"","post_parent":0,"guid":"http:\/\/localhost\/wp\/hergott-0001-4\/","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"0","filter":"raw"}]
So it is basically an object containing 2 objects which are posts. How do i address the attributes of the posts here?