Fetch all Posts where logged in user has commented

I’m not good at sql queries so the question is:

How do I list all the Posts where logged in user has left the comment?

Read More

I guess it’s kinda obvious that redundancy is unwanted in case user posted multiple comments to the same Post.

Thanks!

Related posts

Leave a Reply

1 comment

  1. It should be simple since comments are stored with a user_id if they are created by a logged in user and if not then its set to false so all you need to do is select the post id from comments that are made by user_id bigger the zero, something like this:

    function get_posts_with_loogedin_user_comments(){
        global $wpdb;
        $sql = "SELECT DISTINCT  comment_post_ID
                FROM  $wpdb->comments
                WHERE  user_id > 0
                ";
        $post_ids = $wpdb->get_col($sql);
        if ($post_ids)
            return $post_ids;
        return false;
    }