Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created November 3, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/e18ee6cb5ad8ec7bf91e to your computer and use it in GitHub Desktop.
Save tommcfarlin/e18ee6cb5ad8ec7bf91e to your computer and use it in GitHub Desktop.
[WordPress] Modifying the WordPress query to select distinct records.
<?php
add_filter( 'posts_distinct', 'acme_select_distinct_search_records' );
/**
* Adds 'DISTINCT' to the query that's executing on the search page.
*
* @param string $distinct The initial DISTINCT clause.
* @return string $distinct The 'DISTINCT' keyword, if we're on the search template.
*/
function acme_select_distinct_search_records( $distinct ) {
if ( is_admin() || ! is_search() ) {
return $distinct;
}
return 'DISTINCT';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment