Make WordPress Core

Opened 10 years ago

Closed 9 years ago

#33170 closed defect (bug) (invalid)

get_comments interface doesn't return comments with 'comment' type

Reported by: caramon's profile caramon Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.2.3
Component: Comments Keywords: reporter-feedback
Focuses: Cc:

Description

Hi,

I'm using a theme which gets comments via get_comments interface. The call looks like this:

$comment_entries = get_comments(array('type' => 'comment', 'post_id' => $post->ID));

I noticed that a post has some comments but theme isn't display them. After some debugging I found that the call above sends invalid SQL query to DB. The query looks like this:

SELECT * FROM wp_comments

WHERE
  ((comment_approved = '0' OR comment_approved = '1')) AND comment_post_ID = 234 AND comment_type IN ('')
ORDER BY
  wp_comments.comment_date_gmt DESC

The problem with SQL is content_type in value is empty. In the DB records wp_comment.comment_type has 'comment' value, so it simply doesn't match to SQL that WP generates.

I managed to fix the problem by changing wp-include/comment.php file. The diff looks like this:

  • comment.php

    old new  
    741741
    742742                                       case 'comment':
    743743                                       case 'comments':
    744                                                $comment_types[ $operator ][] = "''";
     744                                               $comment_types[ $operator ][] = "'comment'";
    745745                                               break;
    746746
    747747                                       case 'pings':

I don't think that my solution is the right one but I think you should get an idea how the bug can be fixed. :)

Thanks for reading it.

Change History (4)

#1 @boonebgorges
10 years ago

  • Keywords reporter-feedback added

Hi caramon - Thanks for the report, and welcome to WordPress Trac!

After some debugging I found that the call above sends invalid SQL query to DB.

comment_type IN ('') is not invalid SQL. It looks for rows where the comment_type field is an empty string.

In the DB records wp_comment.comment_type has 'comment' value, so it simply doesn't match to SQL that WP generates.

Normal WordPress comments - those created via wp-comments-post.php - have comment_type = ''. See https://core.trac.wordpress.org/browser/tags/4.3/src/wp-comments-post.php?marks=119#L118. If you have records in your database where the value in the comment_type field is 'comment', it might be a plugin at work. Can you verify this behavior with all plugins disabled?

This ticket was mentioned in Slack in #core by boone. View the logs.


9 years ago

#3 @boonebgorges
9 years ago

  • Component changed from Query to Comments

#4 @rachelbaker
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

@caramon Welcome to Trac. As Boone mentioned above the query is working as intended, "comments" have a comment_type of '' in the database (weird, I know!).

I am closing this ticket, but feel free to re-open if I am mis-understanding.

Note: See TracTickets for help on using tickets.