【问题标题】:SQL: Wordpress users ordered by the date of their latest post (CPT)SQL:Wordpress 用户按最新发布日期 (CPT) 排序
【发布时间】:2016-10-08 01:26:12
【问题描述】:

需要获取以特定自定义帖子类型 (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)。

我怎样才能做到这一点?

谢谢。

【问题讨论】:

    标签: mysql sql wordpress plugins custom-post-type


    【解决方案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

    【讨论】:

    • 谢谢你的回答,我觉得应该是ORDER BY 'post_date'吧?为了在他们最新发布的日期之前订购它。当不与 GROUP BY 'post_author' 一起使用时,它有点工作。如果与GROUP BY 'post_author' 一起使用,它会打乱顺序。但是如果GROUP BY 'post_author'没有被使用,而一个用户最近恰好发了两次,他也会在列表结果中出现两次。请帮忙!
    • 如果我不使用GROUP BYCOUNT(ID),它就像一个魅力。但是,正如我所提到的,如果一个用户最近碰巧发布了两次,他也会在列表结果中出现两次。另外,如果可能的话,我想获取用户的总帖子(使用 COUNT() 或类似的)。感谢您的帮助
    • 我需要 5 个用户的列表,按他们最近的 CTP 帖子排序。这在没有GROUP BY 'post_author' 的情况下工作。但是如果我们不使用它,如果一个用户最近两次发帖,他也会在列表结果中出现两次。另外,如果可能的话,我想获取用户的总帖子(使用 COUNT() 或类似的)。
    • 好的,我可以不用计算帖子总数。但是,请帮助让 5 位用户按他们的最新帖子排序。请帮忙!谢谢
    • 我想获取用户的帖子总数,有点像一枪杀死 2 只鸟,但我可以忽略这一点,稍后使用其他功能获取每个用户的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多