问题
@Dianuj solved my issue a week ago, when I asked how I can get a recent comment
above a post
. That all worked very well, but yesterday, when I tested it, I saw a little issue, which I not want on my Wordpress
site. Let me explain it to you...
If someone makes a comment on a post that has none comments before you comment, then the function on this page How to get most recent commented post above new submitted post in Wordpress? will work flawlessly. I thank Dianuj for making my life easier.
The problem starts when a user makes a comment on a post that ALREADY has a comment in it. So for instance, you make the second comment on a post. In that case, the post title won't go up and show 'his' face on the page. That is the big problem and I hope someone can help me out with the snippets that are on this link How to get most recent commented post above new submitted post in Wordpress?
PS: for the lazy ones, here it goes -->
<?php
global $wpdb;
$results = $wpdb->get_results(" SELECT p.*,
(CASE WHEN c.comment_date IS NULL THEN p.`post_date` ELSE c.comment_date END) order_column
FROM `wp_posts` p
LEFT JOIN `wp_comments` c ON (p.ID = c.`comment_post_ID` ) WHERE p.post_type='post' AND p.post_status='publish'
GROUP BY p.ID
ORDER BY order_column DESC");
?>
回答1:
In above case GROUP BY
groups the data with first occurrence of the p.ID
so the latest will be neglected here is the trick to get all data with the ORDER BY
and then group the data so the latest one will be there
Try this one
SELECT * FROM (
SELECT p.*,
(CASE WHEN c.comment_date IS NULL THEN p.`post_date` ELSE c.comment_date END) AS order_column
FROM `wp_posts` p
LEFT OUTER JOIN `wp_comments` c ON (p.ID = c.`comment_post_ID` ) WHERE p.post_type='post' AND p.post_status='publish'
ORDER BY order_column DESC
) t GROUP BY ID ORDER BY order_column DESC
来源:https://stackoverflow.com/questions/17725112/most-recent-post-does-not-come-above-post-if-there-was-already-made-an-comment