Come together with the global Drupal community in Rotterdam, 28 Sept – 1 Oct 2026. Sessions, contribution, connection, and Early Bird savings until 8 June.
I tried the following code to include comments in the general node search, but commented it out as it led to no results being returned. The code adds c.subject and c.comment to the list of fields being selected and creates a join between the comments and node fields. Any suggestions as to why this doesn't work?
// When comments are included, no results are returned. What is the problem?
// if (module_exist('comment')) {
// $query->fields[] = 'c.subject';
// $query->fields[] = 'c.comment';
// $query->joins .= ' INNER JOIN {comments} c ON n.nid = c.nid';
// }
I also noticed this comment in function node_trip_search()
// When comments are included, no results are returned. What is the problem?
// if (module_exist('comment')) {
// $query->fields[] = 'c.subject';
// $query->fields[] = 'c.comment';
// $query->joins .= ' INNER JOIN {comments} c ON n.nid = c.nid';
// }
I think the reason is that this is an INNER JOIN. So it tries to join nodes to comments, and since not all nodes have comments, this does not work.
Comments
Comment #1
nedjoI tried the following code to include comments in the general node search, but commented it out as it led to no results being returned. The code adds
c.subjectandc.commentto the list of fields being selected and creates a join between thecommentsandnodefields. Any suggestions as to why this doesn't work?Comment #2
kbahey commentedNedjo
I also noticed this comment in function node_trip_search()
// When comments are included, no results are returned. What is the problem?
// if (module_exist('comment')) {
// $query->fields[] = 'c.subject';
// $query->fields[] = 'c.comment';
// $query->joins .= ' INNER JOIN {comments} c ON n.nid = c.nid';
// }
I think the reason is that this is an INNER JOIN. So it tries to join nodes to comments, and since not all nodes have comments, this does not work.
Try a LEFT JOIN instead and see if it works.
Comment #3
nedjoYep, thanks kblahey, that worked.
Comment #4
(not verified) commentedComment #5
(not verified) commentedComment #6
(not verified) commentedComment #7
(not verified) commentedComment #8
(not verified) commentedComment #9
(not verified) commentedComment #10
(not verified) commentedComment #11
(not verified) commentedComment #12
(not verified) commented