Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: add SELECT DISTINCT facilities #1301

Open
lkraav opened this issue Nov 26, 2021 · 0 comments
Open

Query: add SELECT DISTINCT facilities #1301

lkraav opened this issue Nov 26, 2021 · 0 comments

Comments

@lkraav
Copy link
Contributor

lkraav commented Nov 26, 2021

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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant