Note: You are viewing the documentation for Version 4View Version 3 Docs

Sort Posts, Pages, Custom Post Types by Date

By default SearchWP sorts results in order of relevance from most relevant to least. Using a SearchWP Mode we can override the sorting of results to instead return in the order they were published.

All hooks should be added to your custom SearchWP Customizations Plugin.

// Sort SearchWP Post, Page, and Custom Post Type Results by date in DESC order.
add_filter( 'searchwp\query\mods', function( $mods ) {
$mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'post' ) );
$mod->order_by( function( $mod ) {
return $mod->get_local_table_alias() . '.post_date';
}, 'DESC', 1 );
$mods[] = $mod;
return $mods;
} );

Note that this hook applies only to WP_Post-based Sources (e.g. Posts, Pages, Custom Post Types) but it can be modified to consider any custom Sources you may be using.

Fix keyword search on your site. No coding required!

Now you can utilize all of the content that's gone unrecognized by native WordPress keyword search instantly with SearchWP

Get SearchWP