{"id":633,"date":"2022-11-11T14:45:25","date_gmt":"2022-11-11T14:45:25","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/caching-data-with-wordpress-transients-api\/"},"modified":"2024-11-04T04:20:42","modified_gmt":"2024-11-04T04:20:42","slug":"caching-data-with-wordpress-transients-api","status":"publish","type":"post","link":"https:\/\/demo.wpartisan.net\/developmentsite\/caching-data-with-wordpress-transients-api\/","title":{"rendered":"Caching data with WordPress Transients API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Before starting with WordPress Transients API, let&#8217;s talk about the meaning of Transients. It means some things stay in a place for only a short time. In our case, we can tell store data in the database for a short time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using WordPress Transients API, we can use wp_options&nbsp;database table to temporarily store cached information in a simple and standardized way by giving a custom name and a time frame after which it will expire and be deleted.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Transients API is very similar to the&nbsp;Options API,&nbsp;but the difference is it added the feature of an expiration time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, anyone who needs to cache specific data but wants it to be refreshed within a given time, they can use WordPress Transients API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can use Transients API functions to store and retrieve information from the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Set\/Get Transient:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>set_transient()<\/li>\n\n\n\n<li>get_transient()<\/li>\n\n\n\n<li>set_site_transient()<\/li>\n\n\n\n<li>get_site_transient()<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Delete Transient:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>delete_transient()<\/li>\n\n\n\n<li>delete_site_transient()<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The &#8220;site_&#8221; functions work network-wide when using WordPress\u00a0Multisite.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s see how can we save and fetch Transients and where we can use it with an real example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before going to the example, we need to understand the parameters of set_transient() API function.<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">set_transient( $transient, $value, $expiration );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can see more details about the parameters <a href=\"https:\/\/developer.wordpress.org\/apis\/handbook\/transients\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s see a real example where we can use WordPress Transients API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose you have a website built with WordPress. On your home page, there is a section that shows the 5 latest articles. We can do a query like this :<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">$args = array(\n\t'post_type' => 'post',\n\t'post_status' => 'publish',\n\t'posts_per_page'=> 5,\n);\n\n$latest_articles = new WP_Query( $args );\n\n\/\/ The Loop\nif ( $latest_articles->have_posts() ) {\n\techo '&lt;ul>';\n\twhile ( $latest_articles->have_posts() ) {\n\t\t$latest_articles->the_post();\n\t\techo '&lt;li>' . get_the_title() . '&lt;\/li>';\n\t}\n\techo '&lt;\/ul>';\n} else {\n\t\/\/ no posts found\n}\n\/* Restore original Post Data *\/\nwp_reset_postdata();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Can you understand, when we are browsing the home page, every time the above query is built and executed on the database to fetch the latest posts even if you add a new article every 5 years? Is it not the wrong way to bring the latest posts? Is it not make your site slow?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">WordPress Transients API comes to solve this problem. We can save the above query in our wp_options table for a certain period of time and can call it from wp_options table when needed like below:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">if ( false === ( $latest_articles = get_transient( 'wa_latest_articles' ) ) ) {\n\t$latest_articles = new WP_Query(\n\t\tarray(\n\t\t\t'post_type' => 'post',\n\t\t\t'post_status' => 'publish',\n\t\t\t'posts_per_page'=> 5,\n\t\t)\n\t);\n\n\t\/\/ Put the results in a transient. Expire after 1 week.\n\tset_transient( 'wa_latest_articles', $latest_articles, WEEK_IN_SECONDS );\n}\n\nif ( $latest_articles->have_posts() ) {\n\techo '&lt;ul>';\n\twhile ( $latest_articles->have_posts() ) {\n\t\t$latest_articles->the_post();\n\t\techo '&lt;li>' . get_the_title() . '&lt;\/li>';\n\t}\n\techo '&lt;\/ul>';\n} else {\n\t\/\/ no posts found\n}\nwp_reset_postdata();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here WEEK_IN_SECONDS is a time constant. In&nbsp;WordPress 3.5, several constants were introduced to express time easily:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">MINUTE_IN_SECONDS  = 60 (seconds)\nHOUR_IN_SECONDS    = 60 * MINUTE_IN_SECONDS\nDAY_IN_SECONDS     = 24 * HOUR_IN_SECONDS\nWEEK_IN_SECONDS    = 7 * DAY_IN_SECONDS\nMONTH_IN_SECONDS   = 30 * DAY_IN_SECONDS\nYEAR_IN_SECONDS    = 365 * DAY_IN_SECONDS<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here there is a problem with expiration time. Can you see I set the expiration time is 1 week. This would be appropriate if you really don&#8217;t care if your new article shows up a week after you add it to your site.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to make sure that the article shows up immediately, then I suggest adding a large expiration time( YEAR_IN_SECONDS ). Then create a function to flush your transient when a new article is published.<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">add_action( 'publish_post', 'wa_flush_latest_articles_transient' );\n\nfunction wa_flush_latest_articles_transient( $post_id, $post ){\n\tdelete_transient( 'wa_latest_articles' );\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will show how we can cache data in WordPress using WordPress Transients API with a real-world example. Here I described everything step by step.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[42],"class_list":["post-633","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-wordpress-api"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"This article will show how we can cache data in WordPress using WordPress Transients API with a real-world example. Here I described everything step by step.","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=633"}],"version-history":[{"count":1,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":2287,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/posts\/633\/revisions\/2287"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}