Sort Search Results by Newest First
Lists search results by publish date, newest first, instead of WordPress's relevance order, which suits news sites and blogs where freshness matters most.
Opens WPIDE on
Code
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() ) {
return;
}
$query->set( 'orderby', 'date' );
$query->set( 'order', 'DESC' );
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.