我使用以下代码查找最新评论的ID。我将值缓存3秒。
function get_most_recent_comment_id() {
global $wpdb;
$transName = 'mostRecentCommentID'; // Name of value in trans.
$cacheTime = 3; // Time in seconds between updates.
// Check if value is in the cache
if (false === ($response = get_transient($transName) ) )
{
$max_comment_id = $wpdb->get_var( "SELECT MAX( comment_id ) FROM wp_comments;");
$response = $max_comment_id;
set_transient($transName, $response, $cacheTime);
}
wp_send_json( $response );
}
然而它一直在回归Null。我做错了什么?
答案 0 :(得分:0)
此行存在一些潜在问题:
$max_comment_id = $wpdb->get_var( "SELECT MAX( comment_id ) FROM FROM wp_comments;");
" FROM"给出两次,并且可能不需要wp_comments之后的分号。尝试:
$max_comment_id = $wpdb->get_var( "SELECT MAX( comment_id ) FROM wp_comments");
答案 1 :(得分:0)
尝试将$ db对象作为参数传递给函数
function get_most_recent_comment_id($db) {
$transName = 'mostRecentCommentID'; // Name of value in trans.
$cacheTime = 3; // Time in seconds between updates.
// Check if value is in the cache
if (false === ($response = get_transient($transName) ) )
{
$max_comment_id = $db->get_var( "SELECT MAX( comment_id ) FROM wp_comments;");
$response = $max_comment_id;
set_transient($transName, $response, $cacheTime);
}
wp_send_json( $response );
}
用$ wpdb对象调用你的函数
function get_most_recent_comment_id($wpdb);