gpt4 book ai didi

php - MySQL - 跳过重复的 WordPress 条目

转载 作者:行者123 更新时间:2023-11-29 02:09:02 25 4
gpt4 key购买 nike

我正在编写一个脚本来显示 10 个最近“活跃”的 WordPress 博客帖子(即那些有最新评论的帖子)。问题是,该列表有很多重复项。我想清除重复项。有没有一种简单的方法可以通过更改 MySQL 查询(如 IGNORE、WHERE)或其他方式来做到这一点?这是我到目前为止所拥有的:

<?php

function cd_recently_active() {
global $wpdb, $comments, $comment;
$number = 10; //how many recently active posts to display? enter here

if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
$comments = $wpdb->get_results("SELECT comment_date, comment_author, comment_author_url, comment_ID, comment_post_ID, comment_content FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");

wp_cache_add( 'recent_comments', $comments, 'widget' );
}
?>

最佳答案

查看DISTINCT SELECT 语句的选项。或者 GROUP BY 语法(查看相同的链接)。尽管它们的工作方式不同,但这是最有可能帮助您准确获得所需内容的两种方法。

关于php - MySQL - 跳过重复的 WordPress 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/306466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com