Skip to content

Instantly share code, notes, and snippets.

@davidperezgar
Created October 18, 2015 21:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidperezgar/bc7732c665812e71ba4a to your computer and use it in GitHub Desktop.
Save davidperezgar/bc7732c665812e71ba4a to your computer and use it in GitHub Desktop.
Wordpress Search Order by date not relevance
//Imported from https://wordpress.org/support/topic/371-i-need-search-to-show-results-by-date-not-relevance
function my_search_query( $query ) {
// not an admin page and is the main query
if ( !is_admin() && $query->is_main_query() ) {
if ( is_search() ) {
$query->set( 'orderby', 'date' );
}
}
}
add_action( 'pre_get_posts', 'my_search_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment