hesam hesam - 1 year ago 76
JSON Question

why this code just load 10 latest posts?

Someone made this code for me to load the posts in json format but it just loads the 10 latest posts. How do I change it to load all the posts?

I tried by?page=2 but it doesn't work.

json url



header("Content-type: application/json");

include ('wp-load.php');

$loop = new WP_Query(array( 'post_status' => 'publish', 'post_type' => 'post'));
if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post();

$posts[] = array(
'id' => $post->ID,
'post_title' => $post->post_title,
'post_content' => $post->post_content,
'guid' => $post->guid,

'image' => (has_post_thumbnail() ? get_the_post_thumbnail_url() : ''),
'cats' => the_category_ID( false ),
'post_date' => $post->post_date,

endwhile; endif;
echo json_encode($posts);


Answer Source

You should have taken a look at docs. There's a pre-set posts_per_page param which is to be set to -1 if you want all the posts:

$loop = new WP_Query(
    array( 'post_status' => 'publish', 'post_type' => 'post', 'posts_per_page' => -1 )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download