You can’t do this through the query passed in ‘pre_get_posts’. You are limited by the options listed in https://codex.ww.wp.xz.cn/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
It can probably be done with a custom SQL query, but such a query is beyond by ability because we’d need a sub-query to first identify the latest comment for each post before ordering the posts.
It’s possible to sort the posts with PHP just before the main loop by running another loop that finds the latest comment date for each post and adds the comment’s timestamp to the associated array element. The post results array can then be sorted on that value before entering the main loop.
(@perthmetro)
10 years ago
How would I edit this to order the posts by the latest comment, or order by comment date.
Thanks,Pete