Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON using wp_Query in wordpress?

I'm trying JSON using wp_Query in wordpress, but i have get error Fatal error: Cannot use object of type WP_Post as array. this my code

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
require('wp-config.php');

//contoh parameter post per kategori >> category_name=kesehatan
//contoh parameter post per kategori >> showposts=5


$posts = array();
$the_query = new WP_Query( 'showposts=5' ); 

while ($the_query -> have_posts()) : $the_query -> the_post(); 

$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail_name');
    $post[thumbnail_post]= $thumb[0]; // Fatal error: Cannot use object of type WP_Post as array
    $post[waktu_post]= get_the_time('Y-m-d H:i');// Fatal error: Cannot use object of type WP_Post as array
    $post[link_detail_post]= the_permalink();// Fatal error: Cannot use object of type WP_Post as array
    $post[judul_post]= title(); // Fatal error: Cannot use object of type WP_Post as array
    $post[ringkasan_post]= the_excerpt(__('(more…)'));// Fatal error: Cannot use object of type WP_Post as array

    $posts[] = $post;
endwhile;

echo json_encode(array('post'=>$posts));

?>

how to fix it ? sorry for my english

like image 639
Amay Diam Avatar asked Aug 02 '26 04:08

Amay Diam


1 Answers

Try renaming the $post variable. The GLOBAL $post variable might be being referenced that's why you get the error.

like image 63
joseconsador Avatar answered Aug 04 '26 18:08

joseconsador