我想获得日期gmt最新评论的帖子ID。结果我想要得到一个字符串。WordPress的SQL查询最后评论日期
有人可以帮我如何设置导致成字符串:
function GetLastCommentDate($postId) {
global $wpdb;
$dateOutput = '0000-00-00 00:00:00';
$commentRes= $wpdb->get_results("SELECT DISTINCT `comment_date_gmt` FROM `wp_comments` WHERE `comment_approved` ='1' AND `comment_post_ID` = '". $postId. "' ORDER BY `comment_date_gmt` DESC LIMIT 1");
if(!empty($commentRes)) {
$dateOutput = ...........
}
return $dateOutput;
}
一个答案是这样的:
$commentRes= $wpdb->get_results("SELECT DISTINCT `comment_date_gmt` as `comment_date_gmt` FROM `wp_comments` WHERE `comment_approved` ='1' AND `comment_post_ID` = '". $postId. "' ORDER BY `comment_date_gmt` DESC LIMIT 1");
if(!empty($commentRes)) {
foreach($commentRes as $comment) {
$dateOutput=$comment->comment_date_gmt;
}
}
return $dateOutput;
但如何避免foreach循环?只有一行(sql限制设置为1)。
你试过'的var_dump($ commentRes);'看它所包含的内容? – jeroen
'SELECT max(comment_date_gmt)FROM wp_comments WHERE comment_approved ='1'AND comment_post_ID ='“。$ postId。”'“'should do – Orangepill