Skip to content

Instantly share code, notes, and snippets.

@barrd
Created December 24, 2022 11:13
Show Gist options
  • Save barrd/b2e5218cf45013e696f029f618d9aec4 to your computer and use it in GitHub Desktop.
Save barrd/b2e5218cf45013e696f029f618d9aec4 to your computer and use it in GitHub Desktop.
WordPress custom search order by date
<?php
/**
* Custom search order by Date
*
* @param string $orderby Orderby string.
* @param object $query The query.
*/
function barrd_sort_search_by_date( $orderby, $query ) {
global $wpdb;
if ( ! is_admin() && is_search() ) {
$orderby = "{$wpdb->prefix}posts.post_type ASC, {$wpdb->prefix}posts.post_date DESC";
}
return $orderby;
}
add_filter( 'posts_orderby', 'barrd_sort_search_by_date', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment