To clarify the above question title, what I am trying to do is create a grid of posts, for which I am displaying a thumbnail of an image that is attached to that post.
Then, upon clicking each of these images in the grid, I want to display the rest of the contents of the post (containing custom field data) via JS underneath the grid item.
All my frontend code is working. However, what I would like to do is set up the backend to query the posts once (initially when creating the grid) then when displaying the preview of each post, pull the remaining fields I need from each post into my preview HTML and display.
So in the backend, what I want to do is:
- When page loads, query DB for posts and store in PHP somewhere. In an array or object.
- Use certain fields to build grid of post thumbnails.
- Upon clicking the thumbnail, make an AJAX call (passing post ID) and return the fields from the array for object for that post ID via JSON.
- Load into preview HTML and display.
So, with that scenario in mind. How do I store the results of my query (in a function) then access that data later via AJAX?
Thanks in advance, Craig
jQuery.data()
. For persisting results in the database you can store the result in a transient, then you would query the transient for the data you need. It would still require that you hit the database but it is computationally less expensive.