WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

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


share|improve this question
up vote 2 down vote accepted

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;
share|improve this answer
That's exactly what I needed! The only thing that I changed is sql WHERE clause because it actually needs to be compared to logged in user ID. Thanks a bunch! – Dameer Mar 24 '13 at 10:30
@Bainternet, can you please paste the code after changes? I am a new bee to Wordpress. Having hard time to achieve this. – N Panigrahy Jan 15 '15 at 17:24
@Nilanchala sorry but the comment above yours was addressed to me and not by me so i have no idea what was the change. i can only assume that he changed user_id > 0 to user_id = 'x' where X is the actual user ID – Bainternet Jan 16 '15 at 8:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.