Custom post types for WordPress support comments, however the API doesn’t allow for retrieving those comments.
So in order to get the latest comments from your custom post type you need to do an sql query that does a LEFT OUTER JOIN between the comments table and the posts table:
/* Get recent comments */ global $wpdb; $sql = "SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND post_type='your_custom_post_type' ORDER BY comment_date_gmt DESC LIMIT 5"; $comments = $wpdb->get_results($sql); foreach ($comments as $comment) { // we need the comment comment user_id in case it's a logged in user so we can echo the display name and not username if ($comment->user_id != 0) { $curent_userdata = get_userdata($comment->user_id); echo $curent_userdata->display_name; } else { echo strip_tags($comment->comment_author); } // the permalink for the comment echo get_permalink($comment->ID) . '#comment-' . $comment->comment_ID; // the post title where that comment was posted echo $comment->post_title; // display the comment content echo strip_tags($comment->comment_content); } |




It’s been a while since I’ve reviewed books on this blog and I think it’s about time to start doing that again. And I couldn’t have chosen a better book to start with then 

join me on twitter
free rss updates
wpMail.me – a concise, once weekly free roundup of WordPress news and articles
wpMail.me is a concise, once-weekly free round up of news and articles. It’s main goal is to ease some of the information overload we encounter in our daily computing by providing with a list of curated articles, news and tutorials. Also since it’s once a week you can batch process it all in 1 hour a week, leaving you with extra free time.
Read More »