I've a site that uses just about a bazillion complex loops...in many instances. For many said loops, I found myself using WP_Query to create an array only to use another WP_Query ad-almost-infinitum to render a very 'filtered-down' end loop. I have the Query Monitor Plugin enabled and the amount of query requests would make anyone that cares about query performance blush...a lot. My end goal: render complex loops fast whilst minimizing database requests.

Note: I've peeked into, but I've not yet had the time to fully delve-into/test/verify WP's Rest API. I also have various caching mechanisms in place

So, what I've done is use WP_Query in conjunction with cron jobs (I have a server cron job, in ~/wp-config.php if have define('DISABLE_WP_CRON', true); set) in order to create static JSON files that update every x (30 mins) intervals.

From there I'm using something a la...

$file = get_home_path() . '/my-dir/my-stuff.json';
$my_json_list = json_decode(file_get_contents($file), true);

...to get a loop for foreach processing.

My questions:

  • Does what I'm doing make any sense?
  • Is there a better way to render complex loops, whilst minimizing database requests?

Huge thanks in advance!

  • How often and under what conditions does the data change? You could generate the query results only when the data changes rather than when it needs to be viewed.
    – Milo
    Jan 25 '17 at 5:05
  • Hi Milo, Thanks for the reply. Some of the content changes quite frequently (< 15 mins). I did think about running the function on content changes instead of fixed cron intervals...but because the content changes so frequently I opted for cron jobs.
    – Ryan Dorn
    Jan 25 '17 at 22:27
  • Hi, Ryan. Can you elaborate a little more about about those queries inside queries? Maybe some more code? All those queries changes with the same 15min frequency? Is it possible to cache queries and output, even fragments? Do you have any kind of persistent cache backend? Jan 25 '17 at 22:57

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.