1

Just wanted to get some other opinions on what everyone thinks might be the most efficient ways of accomplishing the following.

So I have been working on a react-based app that takes advantage of custom endpoints via the REST API to interact with all sorts of CPT and taxonomy data within a WP backend. To accomplish this, the data for each post is stored as a json object within that post's meta field labeled json. Then if specified, another json object is created including the post's ID as well as taxonomy data and adds it to another json object which contains a list of all posts for that CPT. This json object is stored as a transient.

For instance, for a CPT labeled 'courses', whenever an admin adds or edits a course, all of the custom field values, etc. will be encoded as json and saved within that course's json post meta. Right after the post meta is saved, the courses_index transient is retrieved using get_transient(), and that courses ID and other relevant data is added to the index. So we have done two things, converted all info we need for that course into a json object, and also added that course and its taxonomy info to a json index which lists all of the courses on the system. Necessary steps are also taken when the courses is deleted or reverts to a status other than published to remove it from the index.

The purpose here is to avoid complex WP_Querys on the fly when interacting with the frontend. To use the basic example above, say a user navigates to a "Courses" screen which initially lists all courses as well as provides a filter to narrow by a taxonomy. Instead of running a query, the app simply retrieves the json index that is stored as a transient, parses and stores that index within the component's state, and then renders a list of the courses based on that data. Also, then when using the filter we only have to run a function to filter the index of courses we have stored within the state.

Again, the point of this is to reduce the load on the database as much as possible. Although the above example is the most basic of the actual use-cases within the app, I think it is most clear.

So I am curious to get some thoughts on this and some advantages/disadvantages from using queries vs get_transient(), particularly in terms of scalability in maintaining the indexes. In your opinion, is the above the best approach?

  • WordPress already has a internal cache system then it's not sure that you need that. to know if it's usefull you need to test it with a big number of queries. – mmm Jan 22 '18 at 16:59
  • Since I need more persistent caching, as far as I know the above method leverages the Transients_API to accomplish this natively. – KMcAloon Jan 22 '18 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.