WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Issue with json_encode(array) only returning 5 results (11 posts)

  1. ecsancho
    Member
    Posted 5 months ago #

    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.

  2. Andrew Nevins
    Barrel Rider, Spam Zapper & Volunteer Moderator
    Posted 5 months ago #

    Can you post here the function you're using that returns the value you're passing onto JS?

  3. ecsancho
    Member
    Posted 5 months ago #

    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

  4. Andrew Nevins
    Barrel Rider, Spam Zapper & Volunteer Moderator
    Posted 5 months ago #

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

  5. ecsancho
    Member
    Posted 5 months ago #

    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.

  6. Andrew Nevins
    Barrel Rider, Spam Zapper & Volunteer Moderator
    Posted 5 months ago #

    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.

  7. ecsancho
    Member
    Posted 5 months ago #

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

  8. ecsancho
    Member
    Posted 5 months ago #

    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.

  9. ecsancho
    Member
    Posted 5 months ago #

    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.

  10. Andrew Nevins
    Barrel Rider, Spam Zapper & Volunteer Moderator
    Posted 5 months ago #

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

  11. ecsancho
    Member
    Posted 5 months ago #

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

Reply

You must log in to post.

About this Topic

Tags

No tags yet.