wordpress,用他的分类显示所有帖子,但只使用mysql?

ou6hu8tu  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(262)

在phpmyadmin中是否可以只使用mysql来显示所有帖子和他的所有分类?
例如,在一个表中显示所有帖子及其类别:
帖子id,帖子作者,帖子标题,帖子内容,分类id,分类标题

afdcj2ne

afdcj2ne1#

这可以满足你的要求。如果有一个以上的类别附加到该职位,他们将被逗号分隔

SELECT `posts`.`ID` AS `ID`, `posts`.`post_author` AS `author`, `posts`.`post_title`, `posts`.`post_content`, `category_ids` , `category_names` FROM `wp_posts` AS `posts`
LEFT  JOIN 
(
    SELECT `cat_data_re`.`object_id` AS `object_id`, group_concat(DISTINCT `category`.`term_id` SEPARATOR ', ') AS `category_ids`,group_concat(DISTINCT `category`.`name` SEPARATOR ', ') AS `category_names`
    FROM `wp_term_relationships` AS `cat_data_re`
    LEFT JOIN `wp_term_taxonomy` AS `cat_data_t` ON(`cat_data_re`.`term_taxonomy_id` = `cat_data_t`.`term_taxonomy_id`)
    LEFT JOIN `wp_terms` AS `category` ON(`category`.`term_id` = `cat_data_t`.`term_id`) AND `cat_data_t`.`taxonomy` = 'category'
    GROUP BY `cat_data_re`.`object_id`
) AS `cat_data` ON `cat_data`.`object_id`=`posts`.`ID`

如果希望author成为author的显示名,而不仅仅是一个id,请使用以下命令:

SELECT `posts`.`ID` AS `ID`, `users`.`display_name` AS `author`, `posts`.`post_title`, `posts`.`post_content`, `category_ids` , `category_names` FROM `wp_posts` AS `posts`
INNER JOIN `wp_users` AS `users` ON `posts`.`post_author`=`users`.`ID`      
LEFT  JOIN 
(
    SELECT `cat_data_re`.`object_id` AS `object_id`, group_concat(DISTINCT `category`.`term_id` SEPARATOR ', ') AS `category_ids`,group_concat(DISTINCT `category`.`name` SEPARATOR ', ') AS `category_names`
    FROM `wp_term_relationships` AS `cat_data_re`
    LEFT JOIN `wp_term_taxonomy` AS `cat_data_t` ON(`cat_data_re`.`term_taxonomy_id` = `cat_data_t`.`term_taxonomy_id`)
    LEFT JOIN `wp_terms` AS `category` ON(`category`.`term_id` = `cat_data_t`.`term_id`) AND `cat_data_t`.`taxonomy` = 'category'
    GROUP BY `cat_data_re`.`object_id`
) AS `cat_data` ON `cat_data`.`object_id`=`posts`.`ID`

相关问题