Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

WordPress REST API, GET all posts

Hello,

I am building a Dashboard on the front-end which will contain several Highchart charts. In an earlier version of the WP REST API, one could get all posts from the database in JSON. This is no longer possible. Could someone explain using JavaScript, not PHP, how this could be accomplished?

Thank you!

1 Answer

Honestly, This question is too big to answer in the forum, and should be a course here at Treehouse, but to get started, do you have the app postman? if not you will need that. I take it you've been using the REST API a little bit as of now?

What was your process before, that way we can see what you are doing right now and what needs to be changed. Are you authorizing? JWT, OAuth 2?

Thanks for taking the time to respond, Jacob. After much searching, I found that the jqXHR object contains the number of pages for the specified post type. The following, see below, is what I've done. I have no idea if this is an efficient way of pulling back the post data especially if the database contains millions of posts? With regard to your question about authentication, that's something that I have not been able to get my arms around beyond the basic authentication. Do you know of a good resource to implement OAuth2 for a relative beginner/intermediate person.

By all means, I would appreciate your feedback...

            /*----------------------------GET DATA----------------------------------*/  

                var allUsersCalibrations = [];
                var counter = 0;
                var total_pages = 0;
                var allCalibrationsBaseURL = url;

                function allCalibrations(url) {
                    $.ajax( {
                        url: url,
                        type: "GET",
                        cache: false,
                        contentType: 'application/json',
                        dataType : 'json',
                        data:   data,
                        beforeSend: function ( xhr ) {
                            xhr.setRequestHeader( 'X-WP-Nonce', apiNonce );
                        }
                    }) 
                        .done(function(data, textStatus, jqXHR ) {
                            if (counter === 0) {
                                total_pages  = parseInt( jqXHR.getResponseHeader('X-WP-TotalPages'), 10 );
                                console.log("Total Pages: " + total_pages);
                            }

                            allUsersCalibrations.push(data);

                            while (total_pages > 0) {
                                counter++;
                                total_pages--;
                                if (total_pages !== 0) {
                                    newURL = allCalibrationsBaseURL + '?page=' + total_pages;
                                    allCalibrations(newURL);                                
                                }
                                if (total_pages === 0) {
                                    console.dir(allUsersCalibrations);
                                }
                            }
                        })    
                        .fail(function() {
                            console.log("Getting all calibrations failed!");
                        })
                        .always(function() {
                            console.log('allCalibrations Complete');
                        });

                }  // End of allCalibrations()

                allCalibrations(allCalibrationsBaseURL);