通过最新评论订购Wordpress帖子 [英] Ordering Wordpress posts by most recent comment

查看:65
本文介绍了通过最新评论订购Wordpress帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过最新评论来订购Wordpress帖子.据我所知,使用WP_Query对象是不可能的,并且需要自定义$ wpdb查询,我可以轻松编写该查询.但是,我然后不知道如何设置循环以运行该对象.

I'm wanting to order Wordpress posts by the most recent comment. To the best of my knowledge this isn't possible using the WP_Query object, and would require a custom $wpdb query, which I can easily write. However, I then don't know how to setup the loop to run off this object.

任何人都可以帮忙吗?

推荐答案

好的,

这里有很多很棒的答案,但显然没有人花时间来测试它们.

A lot of great answers here, but obviously nobody's taken the time to test them.

郝炼获得了第一个最佳原始答案,但是不幸的是,他的代码中没有注释而没有显示帖子.

Hao Lian gets the credit for the first best original answer, but unfortunately his code doesn't show posts without comments.

Keytar上尉处在正确的轨道上,但是他的代码将每个帖子和附件显示为单独的结果.

Captain Keytar is on the right track, but his code will display every single post and attachment as a separate result.

这里是Keytar船长的修改版本,但将结​​果限制为已发布的"post"类型(以避免获取草稿!)

Here is a modified version of Captain Keytar but it limits the results to the type 'post'.. that has been published (to avoid getting drafts!!)

    select wp_posts.*,
    coalesce(
        (
            select max(comment_date)
            from $wpdb->comments wpc
            where wpc.comment_post_id = wp_posts.id
        ),
        wp_posts.post_date
    ) as mcomment_date
    from $wpdb->posts wp_posts
    where post_type = 'post'
    and post_status = 'publish' 
    order by mcomment_date desc
    limit 10

这篇关于通过最新评论订购Wordpress帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆