We’ve covered the reasons why using transients (and caching in general) can greatly enhance the performance of WordPress sites. I’m offering up what I find to be two compelling solutions for pain points that are often encountered when working with transients–how to create dynamic keys and delete transients in bulk. Creating Dynamic Keys Let’s say I have a function that accepts an array of three arguments and tries to get a transient that is specific to those parameters. If found, the transient’s data will be used. If not, the data will be regenerated. One common way to do this is as follows (see line 24 in particular): * Get the number of posts. * @param array $args { * @type string $post_type The post type. * @type string $after_date The earliest date to get posts for. * @type string $before_date The latest date to get posts for. * @return int|bool The number of posts. function wds_get_post_count( $args ){ // The default arguments. $defaults = array( 'post_type'=>'post', 'after_date'=>'2016-1-1', 'before_date'=>'', // Merge arguments passed in with the defaults. $args = wp_parse_args( $args, $defaults
Share This