stream icon indicating copy to clipboard operation
stream copied to clipboard

Query: add SELECT DISTINCT facilities

Open lkraav opened this issue 3 years ago • 0 comments
trafficstars

Feature Request

I'm trying to fetch a set of unique activities from Stream DB with wp_stream_get_instance()->db->get_records().

It turns out, we have no facilities to set a SELECT DISTINCT clause in https://github.com/xwp/stream/blob/3.8.2/classes/class-query.php, in the spirit of https://developer.wordpress.org/reference/hooks/posts_distinct/

We probably should have such, right?

EDIT current workaround

        /**
         * Custom SELECT DISTINCT query implementation.
         *
         * @see https://github.com/xwp/stream/issues/1301
         */
        add_filter( 'wp_stream_db_query', static function( string $query, array $args ): string {

            // @see https://stackoverflow.com/questions/1252693/using-str-replace-so-that-it-only-acts-on-the-first-match
            if ( isset( $args['_cxl_select_distinct'] ) ) {
                $query = substr_replace( $query, 'SELECT DISTINCT', (int) strpos( $query, 'SELECT' ), strlen( 'SELECT' ) );
            }

            return $query;

        }, 10, 2 );

lkraav avatar Nov 26 '21 01:11 lkraav