mysql - SQL:Wordpress 用户按最新帖子的日期排序 (CPT)

标签 mysql sql wordpress plugins custom-post-type

需要获取在特定自定义帖子类型 (CTP) 中发帖的最后 5 位用户。由于我似乎无法找到一个 WordPress 函数来执行此操作,因此我正在尝试编写一个自定义查询。

到目前为止,我已经得到了这个以获得最后五个和他们的帖子数量(仅适用于 my_custom_type),但它还没有工作。
这是我的查询:

SELECT *, count(ID) 
FROM wp_posts 
WHERE post_type = 'my_custom_type' 
GROUP BY post_author 
ORDER BY post_author DESC LIMIT 5

我想跳过管理员 (post_author=1)。

我怎样才能做到这一点?

谢谢。

最佳答案

你需要通过 IDpost_datepost_author 更好地订购,不包括管理员 post_author>1:

SELECT *
FROM `wp_posts` 
WHERE `post_author` != 1 
AND `post_status` = 'publish'
AND `post_type` = 'my_custom_type' 
GROUP BY `post_author` 
ORDER BY `ID` DESC, `post_author` ASC LIMIT 5

更新:

您现在将获得按上升日期(ASC“ID”)排序的最后 5 位作者列表,这些作者有“已发布”的帖子(自定义帖子类型 =“my_custom_type”),不包括管理员(用户 ID = 1)。最后是每个作者的帖子总数。

这里是查询:

select t1.*, t2.author_count
from `wp_posts` t1
inner join (
    select max(`ID`) as `ID`, `post_author`, count(1) as author_count
    from `wp_posts`
    where `post_author` != '1'
    and `post_status` = 'publish'
    and `post_type` = 'my_custom_type'
    group by `post_author`
) t2 on t1.`ID` = t2.`ID` and t1.`post_author` = t2.`post_author` 
order by t1.`ID` desc limit 5

author_count 是生成的列,用于统计 'published' 帖子总数,每个选定的作者都有一个 'post_type' = 'my_custom_type' .

基于 this answer .

关于mysql - SQL:Wordpress 用户按最新帖子的日期排序 (CPT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37691287/

相关文章:

PHP 会跳过检查并插入数据,即使数据已经存在

PHP session 在刷新时不保存

Mysql效率场景

sql - 这个声明是如何运作的?

mysql - 如何通过多个 JOIN 提高查询性能

mysql - 连接和 SUMS?

java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException - 字段列表中的未知列

html - 具有可变页面内容的侧边栏高度

javascript - Ajax分页wordpress

javascript - WooCommerce 将 HTML 表导出到 Excel