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/Administratorget_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!
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 thewp_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 usewp_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.