I'm creating a website that has two basic user groups: Subscribers and Editors. Bot can create posts via a custom form using wp_insert_post(), but Editors are allowed to mark a post as private, effectively sharing a single post only among other Editors.

As Wordpress already filters private posts out of the loop for anyone lower than Editors, i'm wondering how this will affect my caching because (as far as I understand) I'll have two caches: one showing posts to Subscribers, and one showing posts to Editors which also includes private posts.

Within the loop I'm using:

  • current_user_can(): to include or exclude some post content based on Subscriber/Editor/Administrator
  • get_post_status(): to include or exclude some post content based on if the post is "private" or "published"
  • get_post_meta(): to retrieve various custom fields

Right now I'm using 26 queries in 0.198 seconds on index.php and 28 queries in 0.193 seconds on single.php.

How would one effectively cache this? I don't have any caching plugin yet but I do have access to xCache.

Many thanks!

  • Install W3 Total Cache, enable object caching and select XCache (don't check page caching) – onetrickpony Aug 21 '11 at 13:41
  • Or try "Cachify" from Sergej Müller. (And please use the code-button when writing Qs). – kaiser Aug 21 '11 at 14:29
  • That plugin seems to store the entire output buffer into the options database table using transients. That's more like anti-caching... – onetrickpony Aug 21 '11 at 15:01
  • It also got an option to use the disk cache. – kaiser Nov 4 '12 at 16:56

So your problem is that you want to cache 2 Loops, WordPress do some stuff behind the hood to cache loops. What you might do is either activate a plugin like W3 Total Cache, but if you really need to perform caching of data for some of functionality there is some function/methods that will allow you utilize theses caching "systems" without needing to dive in to documentation.

So there are three basic functions that you might need to check out, first of all you need to wp_cache_set, that will utilize the idea of groups of caching information and keys, so you can retrieve the information later on with the wp_cache_get, and when you are done with that information, and you are sure you will not need it any more later, you can just use wp_cache_delete, to erase that data from your caching "system".

Just a heads up, theses functions are not persistent caching, they will be deleted when you do a new request, but they are saved in memory which means they are very fast, so you store information that you will use later from the database there.

I've put some links in the text to the trac were this functions are defined, but if you want you can take a look at the WP_Object_Cache reference and check how they can be used, and there is some good recommendations also.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.