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`
1条答案
按热度按时间afdcj2ne1#
这可以满足你的要求。如果有一个以上的类别附加到该职位,他们将被逗号分隔
如果希望author成为author的显示名,而不仅仅是一个id,请使用以下命令: