Skip to content

Instantly share code, notes, and snippets.

@rfmeier
Last active July 24, 2019 18:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfmeier/6139865 to your computer and use it in GitHub Desktop.
Save rfmeier/6139865 to your computer and use it in GitHub Desktop.
Append a GROUP BY clause to the current WP_Query SQL clause for the search.
<?php
/**
* Do not include php tags
*/
add_filter( 'posts_groupby', 'custom_posts_groupby', 10, 2 );
/**
* Callback for WordPress 'posts_groupby' filter.
*
* Set the GROUP BY clause to post IDs.
*
* @global $wpdb
* @see https://codex.wordpress.org/Class_Reference/wpdb
*
* @param string $groupby The GROUPBY caluse.
* @param WP_Query $query The current WP_Query object.
* @return string The GROUPBY clause.
*/
function custom_posts_groupby( $groupby, $query ) {
global $wpdb;
//* if is main query and a search...
if ( is_main_query() && is_search() ) {
$groupby = "{$wpdb->posts}.ID";
}
return $groupby;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment