user1082764
user1082764

Reputation: 2015

how to send title and permalink through JSON using get_posts

This is what i print out.

$args = array(
'category' => 'Animation',
'numberposts' => 8
);
$posts_array = get_posts( $args );
echo json_encode($posts_array);

this is the result: (there are 8 identical JSON lists like this, just showing 1 for convenience.)

{"ID":554,"post_author":"1","post_date":"2012-12-28 19:17:43","post_date_gmt":"2012-12-28 19:17:43","post_content":"The race to space is on. As nations compete, we follow the progress of a single chimpanzee that's been recruited into the Space Program. His results might prove influential for the better of all mankind. ...will he be up for the challenge","post_title":"Alpha","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"alpha","to_ping":"","pinged":"","post_modified":"2012-12-28 19:17:43","post_modified_gmt":"2012-12-28 19:17:43","post_content_filtered":"","post_parent":0,"guid":"http://localhost/Wordpress%203.4.2/?p=554","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"0","filter":"raw"

but i just want to send the id and the post_content. i keep on getting null values and cant figure out why.

Upvotes: 0

Views: 481

Answers (1)

Igor Parra
Igor Parra

Reputation: 10348

Just filter by the fields you need: (id and the post_content or title and permalink)

$args = array(
    'category'       => 'Animation',
    'numberposts'    => 8
);

$posts_array = get_posts($args);

$send_array = array();
foreach ($posts_array as $key => $value)
{
    $send_array[$key]["ID"]          = $value->ID;
    $send_array[$key]["post_content"]    = $value->post_content;
}

echo json_encode($send_array);
exit;

Upvotes: 1

Related Questions