Family Encyclopedia >> Electronics

Speed ​​up your WordPress by caching custom queries using the Transients API

Oh boy, the title sounds scary, doesn't it? You have nothing to worry about because we will break it all down. Does your theme run custom WordPress queries to display random posts, popular posts, recent posts, etc. in the sidebar or anywhere else? If so, then you should consider using the WordPress Transient API to cache these queries to reduce resource consumption as well as help load time. In this article, we'll show you how to speed up your WordPress site by storing custom queries using the Transients API.

Note:You must understand how WordPress themes work (loops, etc.) in order to follow this post.

So all this cache and transient jargon is going through my head. Well, don't worry, let us explain what it does. Basically, if you're running a site like List25 and you have a loop that displays 6 random posts in your sidebar, the Transient API can help. Every time a user refreshes the page, that custom WP query you have will go to your database and pull 6 random posts. If you're a relatively new site, it shouldn't be too bad. But if you get a LOT of people on your site, it may crash your SQL server and you will see the "Error Establishing Database Connection" screen. By adding a few extra lines of code, you can easily store the results of that query (cache it) for a certain amount of time using the Transients API.

Example of the loop code we had to throw random posts:

 have_posts ()): $ random_query-> the_post (); ?> 

The best thing about our random sidebar post query was that it showed new content every time. So by caching the query for 12 hours we will have the same 6 posts for 12 hours right? Well, we found a work around thanks to the suggestion of our friend Konstantin Kovshenin (@kovshenin). He suggested that instead of using WP_Query, we use get_posts and pull 20 posts instead. Cache the results of that query using the transients API, and then use the array_rand() function to display only 6 posts out of the original 20 randomly. This way we can continue simulating the random effect on the site.

The first thing we did was set the transient. We got the code from the WordPress Codex page.

 // Obtenga cualquier copia existente de nuestros datos transitorios si (false === ($ special_query_results = get_transient ('special_query_results'))) // No estaba allí, regenere los datos y guarde el transitorio $ randargs = array ( 'orderby' => 'rand', 'numberposts' => 20); $ special_query_results = get_posts ($ randargs); set_transient ('special_query_results', $ special_query_results, 60 * 60 * 12); 

Note that 60 * 60 * 12 is the area where you can control the length of the cache. Feel free to change it to whatever you want. Now if we display the $special_query_results using the foreach loop, we will have all 20 posts displayed. Therefore, we need to use the array_rand() function to extract only 6 random elements. We add the code like this:

 $ randomposts = get_transient ('special_query_results'); $ randkey = array_rand ($ randomposts, 6); 

Now this will pull 6 random post IDs from our transient data. However, it will not extract the values ​​for each post. So we had to add these bits of code:

 $ sixposts [0] = $ randomposts [$ randkey [0]]; $ sixposts [1] = $ randomposts [$ randkey [1]]; $ sixposts [2] = $ randomposts [$ randkey [2]]; $ sixposts [3] = $ randomposts [$ randkey [3]]; $ sixposts [4] = $ randomposts [$ randkey [4]]; $ sixposts [5] = $ randomposts [$ randkey [5]]; 

Basically, we create an array of $6 posts where we assign a value to each of those items. I'm not sure if this was the best way to do it, but it worked. If any of you have better suggestions, feel free to post it in the comments.

After doing that, we are now ready to display the loop. Just put the code like this:

 global $ post; // requerido para que funcione para cada persona ($ sixposts como $ post): setup_postdata ($ post); // Todos los artículos van aquí. endforeach 

setup_postdata allows you to use all the loop tags inside this foreach loop like the_permalink etc.

To make it easy for everyone, here is the final code we have:

 'rand', 'numberposts' => 20); $ special_query_results = get_posts ($ randargs); set_transient ('special_query_results', $ special_query_results, 60 * 60 * 12); // Usa los datos como normalmente tendrías ... $ randomposts = get_transient ('special_query_results'); $ randkey = array_rand ($ randomposts, 6); $ sixposts [0] = $ randomposts [$ randkey [0]]; $ sixposts [1] = $ randomposts [$ randkey [1]]; $ sixposts [2] = $ randomposts [$ randkey [2]]; $ sixposts [3] = $ randomposts [$ randkey [3]]; $ sixposts [4] = $ randomposts [$ randkey [4]]; $ sixposts [5] = $ randomposts [$ randkey [5]]; global $ post; foreach ($ se coloca como $ post): setup_postdata ($ post); ?> 

Ta da, now you only do this database query once every 12 hours, no matter how many users visit your site.