Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Group SearchWP results by Source, sort by relevance within each Source group
<?php
// Group SearchWP results by Source, sort by relevance within each Source group.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
$mod = new \SearchWP\Mod();
$mod->order_by( function( $mod ) {
// Search results should be grouped by Sources in this order.
// NOTE: _ALL_ Engine Sources must be included here!
$source_order = [
'user',
\SearchWP\Utils::get_post_type_source_name( 'post' ),
\SearchWP\Utils::get_post_type_source_name( 'page' ),
];
return "FIELD({$mod->get_foreign_alias()}.source, "
. implode( ',', array_filter( array_map( function( $source_name ) {
global $wpdb;
return $wpdb->prepare( '%s', $source_name );
}, $source_order ) ) ) . ')';
}, '', 1 );
$mods[] = $mod;
return $mods;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.