Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#22213 closed defect (bug) (invalid)

split_the_query breaks posts_distinct filter

Reported by: johnbillion's profile johnbillion Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: Query Keywords: has-patch 2nd-opinion
Focuses: Cc:

Description (last modified by johnbillion)

If you add a DISTINCT value to a query using the posts_distinct filter then the functionality which splits a query (#18536) breaks because the values from your DISTINCT field are used instead of the ID field.


add_filter( 'posts_distinct', function( $val ) {
	return 'DISTINCT post_author as pa, ';
} );

When the split query runs it uses $wpdb->get_col() which will return the DISTINCT field values we asked for instead of the ID fields. In the example above we get an array of post author IDs instead of post IDs.

To get around this problem the split query had to be disabled for this request with the split_the_query filter.

Attachments (1)

22213.patch (544 bytes) - added by johnbillion 11 years ago.

Download all attachments as: .zip

Change History (5)

#1 @johnbillion
11 years ago

  • Description modified (diff)

11 years ago

#2 @johnbillion
11 years ago

  • Keywords has-patch added

Patch which grabs the ID fields from $wpdb->get_results() if there's a DISTINCT value in the query.

#3 @johnbillion
11 years ago

  • Keywords 2nd-opinion added

Wait, DISTINCT operates on all the fields you're selecting. TIL. I think that may invalidate this ticket.

#4 @scribu
11 years ago

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

Yes, DISTINCT means "don't give me any duplicate rows" (i.e. rows that contain the exact same combination of values for each column, respectively).

Note: See TracTickets for help on using tickets.