我正在运行一个脚本来从数据库中获取一些帖子。 这是脚本:
private function getItems()
{
$this->dbConnect($detailsTable);
mysql_select_db(DB_NAME);
mysql_query('SET NAMES utf8');
mysql_query('SET CHARACTER SET utf8');
$result = mysql_query('SELECT *
FROM wp_posts, wp_term_relationships,wp_term_taxonomy
WHERE wp_posts.post_status = "publish"
AND wp_term_relationships.object_id = id
AND wp_term_taxonomy.taxonomy= "category"
AND !(wp_term_taxonomy.term_taxonomy_id = 11)
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
ORDER BY wp_posts.post_date DESC LIMIT 25', LINK);
mysql_close(LINK);
$items = '';
while($row = @mysql_fetch_array($result))
{
$title = UTFayar($row['post_title']);
$content = UTFayar($row['post_content']);
$items .= '<item id="'.$row["ID"].'">
<title><![CDATA['.$title.']]></title>
<description><![CDATA['. $content .']]></description>
<pubDate>'.date('D, j M Y H:i:s T', strtotime($row['post_date'])).'</pubDate>
<category>'.$row['post_category'].'</category>
</item>';
}
$items .= '</channel>
</rss>';
return $items;
}
问题是有些帖子属于3类以上。 所以我得到了错误的结果,我连续3次以上相同的帖子。我需要这个帖子,即使只有一个类别只能在我的RSS中显示一次。
EIDTED:
这是正确的代码,如果有人需要它:
private function getItems()
{
$this->dbConnect($detailsTable);
mysql_select_db(DB_NAME);
mysql_query('SET NAMES utf8');
mysql_query('SET CHARACTER SET utf8');
//$result = mysql_query ('SELECT * FROM wp_posts WHERE post_status="publish" and post_category!=17 and post_category!=18 ORDER BY post_date DESC LIMIT 20', LINK);
$result = mysql_query('SELECT
ID
, post_title
, post_content
, post_date
, group_concat(DISTINCT post_category ORDER BY post_category DESC SEPARATOR ", " ) as "categories"
FROM wp_posts, wp_term_relationships,wp_term_taxonomy
WHERE wp_posts.post_status = "publish"
AND wp_term_relationships.object_id = id
AND wp_term_taxonomy.taxonomy= "category"
AND !(wp_term_taxonomy.term_taxonomy_id = 11)
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
GROUP BY ID, post_title, post_content, post_date ORDER BY wp_posts.post_date DESC LIMIT 25', LINK);
mysql_close(LINK);
$items = '';
while($row = @mysql_fetch_array($result))
{
$title = UTFayar($row['post_title']);
$content = UTFayar($row['post_content']);
$items .= '<item id="'.$row["ID"].'">
<title><![CDATA['.$title.']]></title>
<description><![CDATA['. $content .']]></description>
<pubDate>'.date('D, j M Y H:i:s T', strtotime($row['post_date'])).'</pubDate>
<category>'.$row['categories'].'</category>
</item>';
}
$items .= '</channel>
</rss>';
return $items;
}
答案 0 :(得分:1)
问题是你需要以某种方式处理这些类别......将它们卷起来并用逗号显示在列表中可能是处理它的好方法。
mysql有一个很好的函数叫做“GROUP_CONCAT”http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
您的查询将是
SELECT
ID
, post_title
, post_content
, post_date
, group_concat(DISTINCT post_category ORDER BY post_category DESC SEPARATOR ', ' ) as `categories`
FROM wp_posts, wp_term_relationships,wp_term_taxonomy
WHERE wp_posts.post_status = "publish"
AND wp_term_relationships.object_id = id
AND wp_term_taxonomy.taxonomy= "category"
AND !(wp_term_taxonomy.term_taxonomy_id = 11)
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
GROUP BY ID
, post_title
, post_content
, post_date
ORDER BY wp_posts.post_date DESC