I have created a set of short codes to allow users to lay out the display of my custom post type (herein abbreviated CPT) within their own page.
For brevity, lets assume we’re only looking at these three, which I’ve generalized to be more clear:
[CPT_title]
, [CPT_body]
, and [CPT_taxonomy]
Each grabs the post ID from the $_GET, creates a WP_query, and displays the relevant data from the database.
Now, obviously it’s poor programming to query the same data again and again for each shortcode, so I’d like to share this query data between them.
What’s the most appropriate way to do so?
You could make use of a static variable as a sort of “runtime cache” within your function to avoid duplicate database calls. You just need to ensure you build your array/object storage to appropriately deal with the subsequent calls for different post ID’s (ie Post ID might be an index in your array)
-or-