You can use WP_Object_Cache combined with a persistent cache plugin or you can use Transient API.
WP_Object_Cache:
// Random post link
function randomPostlink(){
$cache = wp_cache_get( 'random_tip_link' );
if( $cache ) {
echo $cache;
} else {
ob_start();
$RandPostQuery = new WP_Query(array('post_type'=>array('tip'),'posts_per_page' => 1,'orderby'=>'rand'));
while ( $RandPostQuery->have_posts() ) {
$RandPostQuery->the_post();
echo the_permalink();
}
wp_reset_postdata();
$cache = ob_get_flush();
wp_cache_set( 'random_tip_link', $cache );
}
}
Transient API:
// Random post link
function randomPostlink(){
$cache = get_transient( 'random_tip_link' );
if( $cache ) {
echo $cache;
} else {
ob_start();
$RandPostQuery = new WP_Query(array('post_type'=>array('tip'),'posts_per_page' => 1,'orderby'=>'rand'));
while ( $RandPostQuery->have_posts() ) {
$RandPostQuery->the_post();
echo the_permalink();
}
wp_reset_postdata();
$cache = ob_get_flush();
set_transient( 'random_tip_link', $cache, DAY_IN_SECONDS );
}
}
If you need to cache the query with or without persistent cache plugin, use Transient API which use database for cache results and if a persistent cache is enabled it will use WP_Object_Cache: