使用场景:首页调用最新评论、自定义获取最*的文章的sql语句
1 2 3 4 5 6 7 8 9 10 11 12 | function d4p_get_query_results() { global $wpdb; $data = get_transient('my_transient_key'); if ($data === false) { $data = $wpdb->get_results(' // SQL 语句 // '); set_transient('my_transient_key', $data, 3600 * 24); } return $data; } |
删除缓存方法
1 | delete_transient('my_transient_key'); |
latest 10 posts sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | global $wpdb; $transient_name = 'lc-latest-10-posts'; $post_ids = get_transient( $transient_name ); if ( $post_ids == FALSE ){ $sql = "SELECT post.ID FROM $wpdb->posts post WHERE post.post_type = 'post' AND post.post_status = 'publish' ORDER BY post.post_date DESC LIMIT 0,10"; $results = $wpdb->get_col( $sql ); if( $results ){ foreach ( $results as $result ){ $post_ids[] = $result; } set_transient( $transient_name, $post_ids, 60 * 60 * 6 ); } } |
原文链接:https://xiaohost.com/3648.html,转载请注明出处。
评论0