Last active
October 27, 2024 23:45
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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