Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

Thanks!

share|improve this question

1 Answer 1

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

Your Answer

 
discard

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.