Support » How-To and Troubleshooting » [Resolved] Issue with json_encode(array) only returning 5 results

[Resolved] Issue with json_encode(array) only returning 5 results

  • Greetings

    I’m trying to grab all post and put them in an array to convert to javascript array and I’m using json_encode() to accomplish that.

    My issue is I have 8 post stored and when the array finally gets to javascript I print the length and it returns 5 which are the 5 lastest post.

    I used get_posts() to grab all the post and tested the length and correctly returns 8, then I tested json_encode() array and length return as 8. Finally the I print the javascript array and it appears all 8 post are in the array but only returns 5. What gives? Any help? thanks.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Can you post here the function you’re using that returns the value you’re passing onto JS?

    JS function to print from json to js
    var posts = <?php echo (array) json_encode($posts_array, 20); ?>;

    php scripts to get all post
    $posts_array = get_posts(array(‘showposts’=> -1, ‘posts_per_page’ => -1, ‘numberposts’=>-1));

    I believe it’s the printing to javascript var phase that is failing. Thanks again

    So if you var_dump your $posts_array you have 8 results or 5?

    in my var_dump() shows all 8 post. I copy the result and scan for “post_author” and retuns 8.

    in php when i do $a = json_encode($posts_array); count($a) //8

    So it looks like it works in php and then printing json_encode() to javascript only returns 5.

    Btw weird things can happen when you mix client-side scripting with server-side like this

    – To rule out any funny business can you try putting your compiled JSON into a new JSON file and then load that file through a get method in jQuery?
    You can use PHP to generate the file.

    What’s the best method to convert php array to javascript?

    i’ll give that a shot, because page I’m working in themes/customize section and I’ve ran into another bug with jquery when I’m in that section. It says it’s available but none of the jquery functions work which is another bug I have to look into haha.

    I placed the same code from 3rd post into the index.php and the encoding is working fine, so its only when it’s in the themes/customize which is
    add_action( ‘customize_register’, ‘themename_customize_register’ ); hook
    that encoding is not working.

    Is this the theme you’re using http://wordpress.org/themes/customizr ?

    Ok I figured it out, it was because js var posts which I had declared somewhere else. Thanks Andrew for the swift replies

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Issue with json_encode(array) only returning 5 results’ is closed to new replies.