这个WP_Comment_Query
班supports 订购人:
\'comment_agent\',
\'comment_approved\',
\'comment_author\',
\'comment_author_email\',
\'comment_author_IP\',
\'comment_author_url\',
\'comment_content\',
\'comment_date\',
\'comment_date_gmt\',
\'comment_ID\',
\'comment_karma\',
\'comment_parent\',
\'comment_post_ID\',
\'comment_type\',
\'user_id\',
\'comment__in\',
\'meta_value\',
\'meta_value_num\',
有一种方法可以通过过滤器进行调整,因此我们可以支持按帖子标题排序:
$args = [
\'status\' => \'approve\',
\'post_status\' => \'publish\',
\'post_type\' => \'post\',
\'orderby\' => \'_post_title\', // <-- Our custom orderby value
\'order\' => \'DESC\'
];
$comment_query = new WP_Comment_Query( $args );
一个简单的演示插件,支持
_post_title
订购可以使用PHP 7:
/**
* Adjust orderby comments clause
*/
add_filter( \'comments_clauses\', function( $clauses, \\WP_Comment_Query $cq ) use ( &$wpdb )
{
$qv = $cq->query_vars;
$orderby = $qv[\'orderby\'] ?? \'\';
$order = $qv[\'order\'] ?? \'ASC\';
if(
\'_post_title\' === $orderby
&& in_array( strtoupper( $order ), [ \'ASC\', \'DESC\' ], true )
)
$clauses[ \'orderby\' ] = " {$wpdb->posts}.post_title {$order},
{$wpdb->comments}.comment_ID {$order} ";
return $clauses;
}, 10, 2 );
然后,为了确保posts表已连接到comment表,如果缺少post type参数,我们可以将其设置为“post”:
/**
* Make usre we have the posts table joined by making sure the post_type isn\'t empty.
*/
add_action( \'pre_get_comments\', function( \\WP_Comment_Query $cq )
{
$qv = &$cq->query_vars;
$orderby = $qv[\'orderby\'] ?? \'\';
if( \'_post_title\' === $orderby && empty( $qv[\'post_type\'] ) )
$qv[\'post_type\'] = \'post\';
} );
请注意,此类查询:
SELECT wp_comments.comment_ID
FROM wp_comments JOIN wp_posts ON wp_posts.ID = wp_comments.comment_post_ID
WHERE ( comment_approved = \'1\' )
AND wp_posts.post_status IN (\'publish\')
AND wp_posts.post_type IN (\'post\')
ORDER BY wp_posts.post_title DESC, wp_comments.comment_ID DESC
由于使用临时文件和文件排序,因此效率不高。
希望您能根据需要进一步调整。